I use maven to build my java based program. It is work fine. But now I meet an issue which request me to build a revision for other users which is based on the same source code with a little different(e.g. different software name, and different resource file).
Does anyone have any idea about how to do it?
You need to use profiles. It's a little complicated to explain in an answer like this, but essentially, you will create different profiles within your POM for the different builds you want to do. You will choose the profile at build time, using, e.g., a variable definition in the mvn command line, and within the profile, you change any of the variables or settings that you need to change. Lots more info is available here.
Depending on the type of project, the most "maven" way to do this would be to split up the project into a parent with multiple children. Your main project is put into one module and each of user specific configurations goes into a different module, which can then depend on the common code.
Each of the user specific modules can have their own resources and unique configuration, which would make producing different named configurations easier. It would also make any user specific coding tweaks easier.
You can create different profiles within your POM for the different builds you want to do.
Here are some of the example part of POM.xml
<!-- Define profiles here and make DEV as default profile -->
<profiles>
<!-- dev Profile -->
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<!-- qa Profile -->
<profile>
<id>qa</id>
<properties>
<env>qa</env>
</properties>
</profile>
<!-- prod Profile -->
<profile>
<id>prod</id>
<properties>
<env>prod</env>
</properties>
</profile>
</profiles>
...
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
Related
I have declared some properties that are specific to Maven profiles. A part of my pom.xml:
<profiles>
<profile>
<id>release</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<my.properties.file>foo.xml</my.properties.file>
</properties>
</profile>
<profile>
<id>ci</id>
<properties>
<my.properties.file>bar.xml</my.properties.file>
</properties>
</profile>
</profiles>
I encounter some problem to use the "ci" Maven profile when I start Junit tests via IntelliJ IDEA 2016.
I activate my profile via the "Maven Projects" panel, then I start tests. The problem is the "my.properties.file" property value is equal to "foo.xml", not "bar.xml".
I have no problem with command-line (I can use the "-Pci" flag). How can I tell IntelliJ to use the "ci" profile? Thx.
You should add the profiles to the Maven setting.xml file (you should find it in the path ${YOUR_MAVEN_HOME}\apache-maven-3.1.1\conf\setting.xml).
Then, you have to open intellij, click on View > Tool Windows > Maven Projects. There, you should see your profiles (ci and release) and select the correct one.
Hope this can help you.
Just finally solved it.
<profile>
<id>profile-to-be-activated-on-build</id>
<activation>
<activeByDefault>false</activeByDefault><!-- on your flavor -->
<property>
<name>mvn-profile-env-var-trigger</name>
</property>
</activation>
</profile>
Goto JUnit default profile (aka configuration template). Add into JVM args:
-Dmvn-profile-env-var-trigger
You may need to manually reload maven profiles in IDE.
Also make sure on [Settings > Build Tools > Maven > Running tests] envVars is checked (or better everything).
I have the following setup:
Jenkins points to -> Pom.xml which points to -> Testng.xml
My goal is to have multiple jobs in Jenkins, each pointing to a different environment. Those environments are currently separated by multiple TestNG files. I have parameters setup in each testng.xml file which point the different environments.
Is there a way to have multiple POM files, each pointing to a different testng file?
Thanks
You should use a single pom file which defines a "profile" for each environment. The profile for an environment will customize all the configuration that is required for that environment (including pointing to the proper testng file).
In your pom, you will include
<profiles>
<profile>
<id>foo</id>
<properties>
...
</properties>
<build>
...
</build>
</profile>
<profile>
<id>bar</id>
<properties>
...
</properties>
<build>
...
</build>
</profile>
</profiles>
and invoke it with
mvn -Pfoo
or
mvn -Pbar
As you say, you will want to have a Jenkins job per environment (with the -Pfoo in the configuration for that job) or you can have a single Jenkins job and pass the profile as a parameter to the job.
I have a multi module web app building with maven. We build the war as per normal and deploy and run on developer machines and local test servers using tomcat.
Then we want to deploy the application to the cloud. To do this we create a special version of tomcat which has all the libraries preloaded and a special version of the war which only has our code. Point here is tomcat is preloaded on the cloud server, the war is uploaded each time it is changed. Currently we are having to manually remove the dependencies from the built war.
What is the best way for maven to do this? Should I build a custom packaging type or maybe run some post build plugin to remove these wars? Or something else? I think the best way to activate this custom build is via a profile. I did try and remove these dependencies by setting them to scope = provided in the new profile but the transitive dependencies still made it into the war.
If you want to exclude all dependencies, you can use the war plugin's packagingExcludes to do so:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
<configuration>
<packagingExcludes>WEB-INF/lib/*.jar</packagingExcludes>
...
</configuration>
</plugin>
Specify this plugin inside a profile to only perform it for production.
You can achieve using profile in maven. As you said it is not working, I can think of you configure something wrong. Try something like:
<profiles>
<profile>
<id>dev</id>
<activation>
<!-- active by default, turn off when on prod -->
<activeByDefault>true</activeByDefault>
</activation>
<dependencies>
<!-- include this in dev, not in prod -->
<dependency>
<groupId>com.company</groupId>
<artifactId>xyz</artifactId>
</dependency>
</dependencies>
</profile>
</profiles>
Then in command line, mvn package -P !dev to deactivate dev profile so that not include the jars.
Make sure com.company:xzy is not included in <project><dependencies></dependencies></project>.
I have a war artefact and I need use some of their classes from a jar.
I can't move the classes to another project, then I deploy the classes and resources included in my webapp as an "attached" artifact using the following configuration:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<attachClasses>true</attachClasses>
</configuration>
</plugin>
This will result in two artifacts being deployed: mywebapp-1.0-SNAPSHOT.war and mywebapp-1.0-SNAPSHOT-classes.jar.
To use those classes I Referencing the artifact as follows:
<dependency>
<groupId>mygroup</groupId>
<artifactId>mywebapp</artifactId>
<version>${project.version}</version>
<classifier>classes</classifier>
</dependency>
When I compiled from Jenkins everything works correctly, but when I run the tests locally from Eclipse can not find the reference classes. (java.lang.NoClassDefFoundError)
I think it might be a bug in the maven eclipse plugin, someone has any idea that can be happening?
Workaround is described on http://wiki.eclipse.org/M2E-WTP_FAQ:
A workaround exists though, we need to change the dependency whether the project is built in Eclipse or not. In your dependent project, you can configure the following :
<dependencies>
...
<dependency>
<groupId>com.company</groupId>
<artifactId>mywebapp</artifactId>
<version>1.0.0-SNAPSHOT</version>
<classifier>${webClassifier}</classifier>
</dependency>
...
</dependencies>
...
<properties>
...
<webClassifier>classes</webClassifier>
</properties>
...
<profiles>
<profile>
<id>m2e</id>
<activation>
<property>
<name>m2e.version</name>
</property>
</activation>
<properties>
<webClassifier></webClassifier>
</properties>
</profile>
</profiles>
The m2e profile is automatically activated when the project is built with m2e, ignored in other circumstances. In that case only, the dependent project will use an empty classifier to reference the web project, which will be added to the classpath as expected.
My simple answer is the following link to the bug tracking system of Eclipse:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=365419
See the answers inside.
Yes it's a problem with Eclipse itself..
The solution within Eclipse just add the project manually within your workspace to the appropriate project where you need the classes out of your war project.
I'm trying to build an application starting from an Appfuse Archetype, but I get some strange problems. For once I'd like to use a hsqldb for automated unit tests and integration tests, and a mysql db for my manual testing so that I can easily manipulate the data when I need to, so it would be nice to automatically switch profiles during the testing phases. Is there a way to do that?
I'm not sure if this is exactly what you are asking for, but you can do the following to setup multiple filters for your Maven project.
<filters>
<filter>/your/path/filter-${env}.properties</filter>
</filters>
This way you can setup multiple profiles using:
<profiles>
<profile>
<id>local</id>
<properties>
<env>local</env>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<env>test</env>
</properties>
</profile>
</profiles>
You can then run the build with the relevant property file using:
mvn -P <profile id>
This would require having property files located at:
/your/path/filter-local.properties
/your/path/filter-test.properties
Not sure if this can help you at all but you can specify alternative resource files in the /src/test/resources folder which override the ones in /src/main/resources when running tests only.
I define an alternative placeholders.properties file here to specify another db connection to be used by the test phase.