Is it possible to run maven with several profiles?
I have a java class annotated with #WebService. Depending on the maven profile the targetNamespace will change. If I run the
mvn release:prepare release:perform
twice, each time with a different profile, I will achieve what I want but the jar versions will not be same regarding the pom version.
So I thought running the release with 2 profiles could do it. Unfortunately when I enter
-P profile-1, -P profile-2
or
-P profile-1,profile-2
only one profile gets executed.
Here ary ma profiles:
<profiles>
<profile>
<id>profile-1</id>
<properties>
<target-namespace>sample-1.org</target-namespace>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>clean</phase>
<configuration>
<target>
<echo>${target-namespace}</echo>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>profile-2</id>
<properties>
<target-namespace>sample-2.org</target-namespace>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>clean</phase>
<configuration>
<target>
<echo>${target-namespace}</echo>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</profile>
I do print out the target-namespace properties to verify that in fact both profiles are running which is not the case.
Thanks
The second command-line (comma-separated profiles) is more correct, but won't quite do what you're trying to do, given your pom.
When you specify multiple profiles, they are all active in the same maven process -- it doesn't mean "run each goal with the first profile and then again with the second". So in the command that you are running, maven attempts to run with both profiles active.
Unfortunately, the contents of your profiles are overlapping. Well, the properties are. The <build> section is identical in each, which means that it could even be moved outside of the profiles and just declared once.
But the <properties> are completely overlapping ... hopefully it's clear that the target-namespace property cannot have two different values at once.
I wrote an answer below for some ways to address this properties question, which was posed in a very general way. But honestly, for anyone who ever reads this: there might easily be other, better ways to accomplish whatever you're actually trying to do. The OP was clearly attempting to do something more specific and this multiple profile thing was the method they were deciding to try, but they were confused when it wasn't working. A question about how to best solve their actual specific problem would probably have been a more productive question. But just in case this is a good way to solve your problem, here goes.
For the purposes of discussion I'm going to ignore the <build> contents above, which appear completely nonsensical, and assume that you need separate profiles because you'll sometimes want to activate each one independently of the other (maybe when not running release).
You can't use two values at once for the same property. You need to have multiple executions occur instead. You have a few different options for this...
Multiple commands. No changes needed to your pom, but this requires that your profiles have no real applicability to the actions in release:prepare, which you'll only run once. If that's not the case, this won't work. The first two commands could be combined into one but the third one has to be separate:
mvn release:prepare
mvn release:perform -P profile-1
mvn release:perform -P profile-2
Multiple <executions> of the release plugin itself, with different releaseProfiles in each one. This is a minor variation on option 1, but might be more appealing depending on your needs:
...
<artifactId>maven-release-plugin</artifactId>
<executions>
<execution>
<id>id-1</id>
<configuration>
<releaseProfiles>profile-1</releaseProfiles>
</configuration>
</execution>
<execution>
<id>id-2</id>
<configuration>
<releaseProfiles>profile-2</releaseProfiles>
</configuration>
</execution>
...
Because the release plugin isn't bound to any phase (you run it directly), you'll have to specify both executions separately on the command line, like mvn release:prepare release:perform#id-1 release:perform#id-2.
Use unique plugin execution ids within each profile -- then run mvn release:prepare release:perform -P profile-1,profile-2 as usual. You'd also have to change the profiles so that they use different property names. e.g.
...
<profile>
<id>profile-1</id>
<properties>
<namespace-1>sample-1.org</namespace-1>
...
<execution>
<id>antrun-1</id>
<configuration>
<target>
<echo>${namespace-1}</echo>
...
<profile>
<id>profile-2</id>
<properties>
<namespace-2>sample-2.org</namespace-2>
...
<id>antrun-2</id>
<configuration>
<target>
<echo>${namespace-2}</echo>
...
... this assumes that the all the plugin executions are such that they would be fine to run in a round-robin order like this for each phase of the overall build (clean phase = antrun 1 then antrun 2; test phase = surefire 1 then surefire 2; etc).
Due to Maven Documentation your second option is correct:
Profiles can be explicitly specified using the -P CLI option.
This option takes an argument that is a comma-delimited list of profile-ids to use. When this option is specified, the profile(s) specified in the option argument will be activated in addition to any profiles which are activated by their activation configuration or the section in settings.xml.
Hence you need to use next command format:
mvn <goals_list> -P profile-1,profile-2
If behaviour is incorrect there could be wrong profile configuration in your POM file. Could you please provide its content?
-P profile-1,profile-2 should be the right form, if it doesn't work it might be due to some conflicts etc. in the profiles themselves.
As an alternative you could try and play with the <activation> tags in settings.xml to activate the profiles based on some property, e.g.
<activation>
<property>
<name>releaseProfile</name>
</property>
</activation>
Then set the property in your mvn call: mvn -DreleaseProfile release:prepare release:perform. (You might have to pass a value for the property, it's been a while since I used that).
Related
In Gradle, we can specify different PMD configurations (including different rulesets) for the pmdMain and pmdTest source sets. e.g.
pmdMain {
ruleSetFiles = files("$javaBuildSystemRoot/src-pmd-rulesets.xml")
}
pmdTest {
ruleSetFiles = files("$javaBuildSystemRoot/test-pmd-rulesets.xml")
}
We want to be less stringent on test code than main code.
There is a separate maven based project, where we cannot use gradle currently.But for now, we would like to at least apply the 2 different rulesets based on main vs test. It is a single module single project, using the maven PMD plugin.
How do we do this in the Maven pom file?
It is "rather unconventional" to pmd over test sources, but that is not part of the question. :)
Using the executions tag and utilizing maven-pmd-plugin, you can do this with maven.
EDIT: In short & applied to the given input (and maybe more than you wanted/need), it enables/forces you to make both checks in every build:
<project><build><plugins>
<plugin>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.11.0</version> <!-- latest up-to-date -->
<executions>
<execution>
<id>pmd-execution</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rulesets>
<ruleset>/path/to/.../src-pmd-rulesets.xml</ruleset>
</rulesets>
</configuration>
</execution>
<execution>
<id>pmd-test-execution</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<includeTests>true</includeTests> <!-- this defaults to false! -->
<rulesets>
<ruleset>/path/to/.../test-pmd-rulesets.xml</ruleset>
</rulesets>
</configuration>
</execution>
</executions>
</plugin>
...
See also: Can I configure multiple plugin executions in pluginManagement, and choose from them in my child POM?
EDIT 2: If you indeed don't need "both executions" (in 1 build), but only "two configurations" for "different builds", then:
Maven Profiles fits your needs (with profiles ... your "possibilities converge to infinity") !
You would introduce profiles like:
<project>
...
<profiles>
<profile>
<id>pmdMain</id>
<properties>
<myPmdRuleSetLocation>/path/to/.../src-pmd-rulesets.xml</myPmdRuleSetLocation>
<myPmdTestsInclude>false</myPmdTestsInclude>
</properties>
</profile>
<profile>
<id>pmdTest</id>
<properties>
<myPmdRuleSetLocation>/path/to/.../test-pmd-rulesets.xml</myPmdRuleSetLocation>
<myPmdTestsInclude>true</myPmdTestsInclude>
</properties>
</profile>
<profiles>
...
</project>
And use it in your (single execution) pmd-plugin configuration:
...
<ruleset>${myPmdRuleSetLocation}</ruleset>
<includeTests>${myPmdTestsInclude}</includeTests>
...
Please read further on profiles and their activation.
(additionally <profile/> can contain & override <build/> tag!)
For our end-2-end test we need to execute the following logical flow:
Create and set up e2e schema (user) in the database (pre-integration-test)
Run Liquibase to initially populate the schema (pre-integration-test)
Add e2e-specific test data to the DB tables (pre-integration-test)
Start Tomcat (pre-integration-test)
Run the web application in Tomcat (integration-test) using Protractor
Shut down Tomcat (post-integration-test)
Clean up the DB: drop the schema (post-integration-test)
For running SQL the sql-maven-plugin is used, however this flow doesn't fit the regular POM layout:
The SQL plugin has to run during pre-integration-test twice, before and after the liquibase-maven-plugin
The SQL plugin has to run before Tomcat plugin during pre-integration-test, however it has to run after during post-integration-test, so that the DB schema is dropped after Tomcat has shut down.
As far as I could conclude from Maven docs, the order of plugins in the POM defines the order of execution during the same phase, and a plugin cannot be mentioned twice in the same POM.
Question: Is there any way to achieve this, apart from writing a shell script that would invoke Maven multiple times?
P.S. found a similar unanswered question.
Given the sample POM below:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.sample</groupId>
<artifactId>sample-project</artifactId>
<version>0.0.2-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>print-hello</id>
<phase>validate</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<echo message="hello there!" />
</target>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.5.0</version>
<executions>
<execution>
<id>exec-echo</id>
<phase>validate</phase>
<configuration>
<executable>cmd</executable>
<arguments>
<argument>/C</argument>
<argument>echo</argument>
<argument>hello-from-exec</argument>
</arguments>
</configuration>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>print-hello-2</id>
<phase>validate</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<echo message="hello there 2!" />
</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
We are actually configuring:
The maven-antrun-plugin to print the hello there! message
The exec-maven-plugin to print the hello-from-exec message
The maven-antrun-plugin to print the hello there 2! message
Goal executions are all attached to the same phase, validate, and we would expect to be executed in the same defined order.
However, when invoking (the -q option is used to have exactly and only their output):
mvn validate -q
we would have as output:
main:
[echo] hello there!
main:
[echo] hello there 2!
hello-from-exec
That is, for the same phase, Maven executed the defined plugins, however merging all of the defined executions for the same plugins (even if defined as different plugin sections) and then execute them in the order to merged definitions.
Unfortunately, there is no mechanism to avoid this merging. The only options we have for configuring plugins execution behaviors are:
The inherited configuration entry:
true or false, whether or not this plugin configuration should apply to POMs which inherit from this one. Default value is true.
The combine.children and combine.self to
control how child POMs inherit configuration from parent POMs by adding attributes to the children of the configuration element.
None of these options would help us. In this case we would need a kind of merge attribute on the execution element or have a different behavior by default (that is, Maven should respect the definition order).
Invoking the single executions from command line as below:
mvn antrun:run#print-hello exec:exec#exec-echo antrun:run#print-hello-2 -q
We would instead have the desired output:
main:
[echo] hello there!
hello-from-exec
main:
[echo] hello there 2!
But in this case:
We are not attached to any phase
We are invoking directly specific executions (and their configurations) via command line (and via a new feature only available since Maven 3.3.1
You can achieve exactly the same via scripting or via exec-maven-plugin invoking maven itself, but - again - the same would apply: no phase applied, only sequence of executions.
I have the wsimport plugin in my project.
I would like to execute the wsimport. According to the website, the string to execute is "mvn jaxws:wsimport".
Firstly, is this string deductable from the XML ?
The artifact ID is :
<artifactId>jaxws-maven-plugin</artifactId>
and goal :
<goal>wsimport</goal>
so is the artifact-part just the substring of the artifactid leading up to "-maven-plugin" ?
..And when I execute my plugin goal "mvn jaxws:wsimport" does this completely ignore which phase I am in? Ie. is this running outside of the phase? And if no, is there a way to run this standalone?
ie. is there a way I can set the phase to none? (eg [phase]none[/phase]).
Pom code :
<build>
<plugins>
<plugin>
<groupId>org.jvnet.jax-ws-commons</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<id>wsimport-from-jdk</id>
<goals>
<goal>wsimport</goal>
</goals>
<configuration>
<executable>${tool.wsimport}</executable>
<wsdlUrls>
<wsdlUrl>http://WorkPC:8080/server-web/AirlineWS?wsdl</wsdlUrl>
</wsdlUrls>
<packageName>com.bluewalrus</packageName>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
When you issue a command like mvn [plugin]:[goal], it launches Maven out of any lifecycle, so if you do not intend to perform that goal inside a lifecycle, but only via such commands, you shouldn't have any <execution> defined, just place <configuration> right after <version>.
About how Maven can shorten the plugin call (i.e. mvn dependency:tree instead of mvn org.apache.maven.plugins:maven-dependency-plugin:2.1:tree), it is based on several conventions:
When no version defined, it tries to take the latest from available repositories
When the groupId is omitted, it looks among the predefined or user-defined pluginGroups to find a suitable one. See here for more information (Configuring Maven to Search for Plugins)
On the same page, you can see how plugins prefixes are used to shorten the plugin prefix, by using a prefix instead of the artifactId of the plugin. Thirdparty plugins should use [prefix]-maven-plugin construction, and it looks OK here.
And to disable the default execution of a plugin (although it might not be useful in this case), you can use this answer
I have a Java project in Eclipse, with JUnit tests in my src/test directory. I've also added a class to my tests with Caliper microbenchmarks, and I'd like to be able to run these tests from within Eclipse.
As the Caliper code is test code, I've added Caliper as a dependency in Maven in test scope. That makes it show up in the classpath when I run JUnit tests, but I can't see a way to run an arbitrary class with test dependencies in the classpath. What I tried doing was adding a new Run Configuration for a Java Application, thinking I could launch CaliperMain with the right class as a parameter, but the Caliper jar is not on the classpath and I can't see how to add it.
I don't want to move my benchmark code and dependency into the main scope, as it's test code! It seems seriously overkill to move it into a completely separate project.
You should be able to do this with the Maven Exec Plugin. For my project, I opted to make a benchmark profile that can be run with the maven command mvn compile -P benchmarks.
To configure something like this, you can add something along the lines of the following to your pom.xml, specifying scope of the classpath as test using the <classpathScope> tag:
<profiles>
<profile>
<id>benchmarks</id>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<id>caliper</id>
<phase>compile</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<classpathScope>test</classpathScope>
<mainClass>com.google.caliper.runner.CaliperMain</mainClass>
<commandlineArgs>com.stackoverflow.BencharkClass,com.stackoverflow.AnotherBenchmark</commandlineArgs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Alternatively, if you'd like to specify a lot of options for caliper, it is probably easier to use the <arguments> tags:
<executions>
<execution>
<id>caliper</id>
<phase>compile</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<classpathScope>test</classpathScope>
<mainClass>com.google.caliper.runner.CaliperMain</mainClass>
<arguments>
<argument>com.stackoverflow.BencharkClass</argument>
<argument>--instrument</argument>
<argument>runtime</argument>
<argument>-Cinstrument.allocation.options.trackAllocations=false</argument>
</arguments>
</configuration>
</execution>
</executions>
More configuration options (like -Cinstrument.allocation.options.trackAllocations above) can be found here and more runtime options (like --instrument above) can be found here.
Then, if you are using the Eclipse m2 Maven plugin, you can right-click on your project folder and select Run as... -> Maven Build... and enter something like clean install in the Goals input box and benchmarks in the Profiles input box and click Run and you should see the output in your Eclipse console.
It's important to note that I used a local snapshot build of Caliper by checking out the source using git clone https://code.google.com/p/caliper/, which is recommended at the time of this post in order to take advantage of the latest API.
I need to execute some tests with maven, and pass a parameter from the command line.
My java code should get the parameter as:
System.getenv("my_parameter1");
and I define the parameter in the pom.xml file as the example below:
(and latter, I'd modify the pom.xml to get the parameter from the common line mvn clean install -Dmy_parameter1=value1)
but it does not work; System.getenv("my_parameter1") returns null.
how should I define the parameter in the pom.xml file?
pom.xml
<project>
...
<profiles>
<profile>
<properties>
<my_parameter1>value1</my_parameter1>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<id>slowTest</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<skip>false</skip>
<includes>
<include>**/*Test.java</include>
<include>**/*TestSlow.java</include>
</includes>
<properties>
<my_parameter1>value1</my_parameter1>
</properties>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
System.getenv() reads environment variables, such as PATH. What you want is to read a system property instead. The -D[system property name]=[value] is for system properties, not environment variables.
You have two options:
If you want to use environment variables, use the OS-specific method of setting the environment variable my_parameter1 before you launch Maven. In Windows, use set my_parameter1=<value>, in 'nix use export my_parameter1=<value>.
You can use System.getProperty() to read the system property value from within your code.
example:
String param = System.getProperty("my_parameter1");
In you surefire plugin configuration, you can use:
<configuration>
<systemPropertyVariables>
<my_property1>${my_property1}</my_property1>
</systemPropertyVariables>
</configuration>
Which takes the Maven property _my_property1_ and sets it also in your tests.
More details about this here.
I'm not sure if system properties from Maven are automatically passed to tests and/or whether fork mode affects whether this happens, so it's probably a good idea to pass them in explicitly.
Use
${env.my_parameter}
to access the environment variable in the pom.xml.
You can use the help plugin to see which variables are set with
mvn help:system
However the normal properties usage should work too. In the large context however I am wondering... what do you want to do? There might be a simpler solution.
The maven surefire plugin also has an option to set environment variables, just add this to your plugin configuration.
<environmentVariables>
<my_parameter1>value</my_parameter1>
</environmentVariables>
I think this requires that the plugin operates in fork mode, which is the default.