Update war name based on activated maven profile - java

In my pom, I have two profiles.
test1
test2
Now I want my war name to change based on the activated profile.
Expected Result
When test1 profile is activated, war name should be prefix-test1.war.
When test1 and test2 are activated, war name should be prefix-test1-test2.war.
When no profile is activated, war name should be prefix.war.
My POM file ....
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.sbill</groupId>
<artifactId>sbill-wrapper</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<packaging>war</packaging>
<artifactId>executable</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<profiles>
<profile>
<id>test1</id>
<properties>
<rp.build.warname>test1</rp.build.warname>
</properties>
</profile>
<profile>
<id>test2</id>
<properties>
<rp.build.warname>test2</rp.build.warname>
</properties>
</profile>
</profiles>
<build>
<finalName>prefix-${rp.build.warname}</finalName>
</build>
</project>
Right now if I run command mvn clean install the war name is prefix-null.war
. If I run command mvn clean install -P test1,test2 the war name is prefix-test2.war.
The result is different to what expected.

Firstly, to avoid showing null when no profiles, we can provide a default value for the property rp.build.warname, as mentioned in Setting default values for custom Maven 2 properties.
For the case running mvn clean install -P test1,test2, the war name is prefix-test2.war as the value of rp.build.warname is overridden, you may read How are conflicting properties resolved if multiple profiles are activated for more details.
In Order to have multiple values, we can use two properties(rp.build.warname1, rp.build.warname2) instead.
The following pom.xml includes the above changes
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.sbill</groupId>
<artifactId>sbill-wrapper</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<packaging>war</packaging>
<artifactId>executable</artifactId>
<!-- Provide default value here to avoid null in file name -->
<properties>
<rp.build.warname1/>
<rp.build.warname2/>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<profiles>
<profile>
<id>test1</id>
<properties>
<rp.build.warname1>-test1</rp.build.warname1>
</properties>
</profile>
<profile>
<id>test2</id>
<properties>
<rp.build.warname2>-test2</rp.build.warname2>
</properties>
</profile>
</profiles>
<build>
<finalName>prefix${rp.build.warname1}${rp.build.warname2}</finalName>
</build>
</project>

Related

How to set up a maven debug profile for a maven java project

I am trying to debug a maven cucumber-serenity Java project in Eclipse Photon 2018/12.
Please tell me how I can set up maven debug profile and run so that I can debug through the code by setting breakpoints.
It depends on your needs. If you want to debug tests, just use this profile:
<project
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<packaging>...</packaging>
<repositories>
...
</repositories>
<distributionManagement>
...
</distributionManagement>
<profiles>
<profile>
<id>debug</id>
<properties>
<maven.surefire.debug>test</maven.surefire.debug>
</properties>
</profile>
</profiles>
<build>
...
</build>
<dependencies>
...
</dependencies>
</project>
But if this is not the case, then you could simply configure your Eclipse IDE doing this.
Hope it helps.

How to develop a Maven Multi-module application using the Spring Boot framework

I have a maven multi-module Java project that runs perfectly, until I try to include the Spring Boot framework. The main module, called core, gets called OK, but all the rest are unable to implement the interface I declared in Core.java.
This error gets thrown:
[ERROR] /F:/mvnmodularapp/module1/src/main/java/service/impl/ModuleServiceImpl.java:[12,30] cannot find symbol
[ERROR] symbol: class Service
[ERROR] /F:/mvnmodularapp/module1/src/main/java/service/impl/ModuleServiceImpl.java:[13,5] method does not override or implement a method from a supertype
The project structure:
mvnmodularapp/
/pom
mvnmodularapp/core/
/src
/target/core-1.0-SNAPSHOT.jar
mvnmodularapp/module1
/src
/target/module1-1.0-SNAPSHOT.jar
THe file/directory structure:
core/src/java/service/
Service.java
core/src/java/service/impl
CoreServiceImpl.java
module1/src/java/service/impl
ModuleServiceImpl.java
Service.java
public interface Service {
String getName();
}
CoreServiceImpl.java in core
#Service
public class ServiceImpl implements Service {
#Override
public String getName() {
return "Hi. You called me from ServiceImpl in Core";
}
}
ModuleServiceImpl.java in module1
#Service
public class ModuleServiceImpl implements Service {
#Override
public String getName() {
return "Hi. You called me from ModuleServiceImpl in module1, but I can\'t pick up right now. Sorry";
}
}
THE POMS
mvnmodularapp pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mvnmodularapp</groupId>
<artifactId>mvnmodularapp</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>core</module>
<module>module1</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>
core pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>mvnmodularapp</artifactId>
<groupId>com.mvnmodularapp</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>core</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Core</name>
<description>SudenGut application</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<start-class>main</start-class>
<java.version>1.8</java.version>
<tomcat.version>8.0.24</tomcat.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--- this will not be enough to provide a cool app :) -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
module1 pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>mvnmodularapp</artifactId>
<groupId>com.mvnmodularapp</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>module1</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<start-class>baseview.impl.BaseViewImpl.ModuleServiceImpl</start-class>
<java.version>1.8</java.version>
<tomcat.version>8.0.24</tomcat.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--- this will not be enough to provide a cool app :) -->
<dependency>
<groupId>com.mvnmodularapp</groupId>
<artifactId>core</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Dependencies between modules are correctly declared but you have one thing that is not configured as required.
I am not sure it will solve your problem but it could.
You declare in each module :
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
It should be required only for the module which bootstraps the Spring Boot application.
If you want have multiple web applications in the same Spring Boot container, you could read this question.
As you say that it works before adding Spring Boot I think that the problem should be solved with previous recommandation.
But if it is not enough, below are other guessworks which could be the cause of the compilation error. I rely on this error message :
[ERROR]
/F:/mvnmodularapp/module1/src/main/java/service/impl/ModuleServiceImpl.java:[12,30]
cannot find symbol [ERROR] symbol: class Service
The Service class is not imported in ModuleServiceImpl.
If I rely on what you write in your question, you should add :
import service.Service in the imports of ModuleServiceImpl.
The core module doesn't create a JAR with the service.Service class.
So, importing it doesn't solve the problem of the module1 module.
You should perform a mvn clean install from the core module and check that the jar contains the compiled class : service.Service in the good package.

Spring Tool Suite can't import spring library classes

windows 8.1 64bit, jdk 1.8 64bit, spring tool suite 3.8.1 (64bit) installed--- how can i remove red lines from import library files
image link :: http://i.stack.imgur.com/EsKWh.png
You have a maven project. In such a project the libs (dependencies) are controlled mavens pom.xml
If you have not done jet, then add the needed dependency declartions to pom
Then click on your project "demo" with the right mouse button, and click on Maven/Update Project...
A typical minimal pom.xml needed an simple spring-boot-web project looks like this (you can find a lot of other boot poms at https://spring.io/guides/):
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.your.group</groupId>
<artifactId>your-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

Maven project configuration for java tomcat application

I'm having trouble setting up my project with maven. I always get that project must be pom or that it is missing a jar package. So here is what I want to do:
I have a main project that has web-inf folder for web, and can be run on tomcat if i export it as war:
<groupId>com.example</groupId>
<artifactId>main</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
I have a module project that has main dependency so I can use all libraries from main project, but will be used as an extension to main project. I plan to have multiple modules like this:
<groupId>com.example.modules</groupId>
<artifactId>module-blogging</artifactId>
<packaging>pom</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>module-blogging</name>
<parent>
<groupId>com.example</groupId>
<artifactId>main</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
The third project I plan to have a deployable project that will only have main project and only modules I want to deploy in a war file.
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
Is this thing even possible with maven? Because I always get that my project is missing some jar dependencies, or that the aggregation project cannot have war packaging.
i have roughly the same scenario 3 modules one of them is deploy-able
and all 3 modules included int one project
so i configured it by 4 .pom files as follows
main-project.pom
<modelVersion>4.0.0</modelVersion>
<groupId>com.#####</groupId>
<artifactId>#########</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>########</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
<maven.test.skip>true</maven.test.skip>
<###########.version>0.0.1-SNAPSHOT</##########.version>
</properties>
<modules>
<module>#########-web</module>
<module>#########-commons</module>
<module>#########-services</module>
</modules>
</project>
commons-module.pom
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>############</artifactId>
<groupId>com.#####</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>###########-commons</artifactId>
<packaging>jar</packaging>
<dependencies>
ur own depends
</dependencies>
</project>
services-module.pom
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>#######</artifactId>
<groupId>com.########</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
ur depends
</dependencies>
<artifactId>########-services</artifactId>
<packaging>jar</packaging>
</project>
web-module.pom
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.#########</groupId>
<artifactId>##########</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>#########-web</artifactId>
<packaging>war</packaging>
<name>############</name>
<url>http://##########.com</url>
<dependencies>
UR DEPENDS
</dependencies>
<build>
<finalName>##############</finalName>
<outputDirectory>target/${project.artifactId}-${project.version}/WEB-INF/classes</outputDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<version>2.8</version>
<configuration>
<version>5</version>
<defaultLibBundleDir>lib</defaultLibBundleDir>
<unpackTypes>war</unpackTypes>
</configuration>
</plugin>
<!-- configure war plugin to skip webxml -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<!-- jetty plugin -->
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.15.v20140411</version>
</plugin>
</plugins>
</build>
UPDATE
your project structure will be something like that:
main-project
|
----commons-module
|
----services-module
|
----web-module
to build and deploy the project
i'm using jetty for deployment
you can see its plugin in web-module pom
at your project root run that command to build the project and generate the war file mvn clean install
then change directory to be on the web-module and run the following command to deploy the war file mvn jetty:run
I hope it's helpful;

Variable dependency of submodule with property from settings.xml

I have a sample maven project with the following structure:
parent
frontend
backend
Frontend depends on Backend.
Backend has a database driver dependency for testing. However, this dependency should be dependent on the developers settings.xml
<dependency>
<groupId>${database.driver.groupId}</groupId>
<artifactId>${database.driver.artifactId}</artifactId>
<version>${database.driver.version}</version>
<scope>test</scope>
</dependency>
In my settings.xml I have this:
<profile>
<id>database</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<database.driver.groupId>mysql</database.driver.groupId>
<database.driver.artifactId>mysql-connector-java</database.driver.artifactId>
<database.driver.version>5.1.9</database.driver.version>
</properties>
</profile>
When I run only the backend the properties are correctly substituted.
However, if I run frontend (or parent) transitive dependencies are not available for frontend because it can't substitute the properties anymore:
[WARNING] The POM for de.phil.mvntest:business:jar:0.0.1-SNAPSHOT is invalid, transitive dependencies (if any) will not be available: 2 problems were encountered while building the effective model for de.phil.mvntest:business:0.0.1-SNAPSHOT
[ERROR] 'dependencies.dependency.artifactId' for ${database.driver.groupId}:${database.driver.artifactId}:jar with value '${database.driver.artifactId}' does not match a valid id pattern. #
[ERROR] 'dependencies.dependency.groupId' for ${database.driver.groupId}:${database.driver.artifactId}:jar with value '${database.driver.groupId}' does not match a valid id pattern. #
The funny part is, that if I move the profile declaration into the parent's pom.xml it works fine!
So my question is, why are the properties of a submodule not substituted when they come from the settings.xml.
Note:
"help:active-profiles -N" shows that the profile from the settings.xml is active.
Note2: The Maven version came with STS and is Embedded 3.0.4
Following the settins.xml and poms
backend
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>de.phil.mvntest</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>backend</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>${database.driver.groupId}</groupId>
<artifactId>${database.driver.artifactId}</artifactId>
<version>${database.driver.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
fontend
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>de.phil.mvntest</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>frontend</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>de.phil.mvntest</groupId>
<artifactId>backend</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
parent
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.phil.mvntest</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<modules>
<module>backend</module>
<module>frontend</module>
</modules>
</project>
settings.xml
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd">
<profiles>
<profile>
<id>database</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<database.driver.groupId>mysql</database.driver.groupId>
<database.driver.artifactId>mysql-connector-java</database.driver.artifactId>
<database.driver.version>5.1.9</database.driver.version>
</properties>
</profile>
</profiles>
</settings>
Manipulating dependencies in profiles isn't very safe. The only case in which it works fairly well is to run tests against different dependencies, and even that is safer with the maven-invoker-plugin instead. The pom that gets deployed can only have one dependency set.

Categories