Passing arguments to Maven release build - java

I'm trying to release a library using Maven and perform a site-deploy to sourceforge (I have create an interactive shell first). The release is done by a Jenkins job (using the Maven Release Plugin for Jenkins).
I tried:
-X -e -Dresume=false -Dusername=puce release:prepare release:perform -Darguments="-Dusername=puce"
and
-X -e -Dresume=false -Dusername=puce -Darguments=-Dusername=puce release:prepare release:perform
but both times the job hangs at site:deploy of the first module:
[INFO] --- maven-site-plugin:3.2:deploy (default-deploy) # myproject-parent ---
[INFO] Parent project loaded from repository: myGroupId:myOtherproject-parent:pom:1.0
[INFO] Parent project loaded from repository: myGroupId:myOtherproject-parent:pom:1.0
Using private key: /opt/jenkins/.ssh/id_dsa
When I stop the job, the following gets printed at end:
Password for ${username}#shell.sourceforge.net: channel stopped
which probably means that ${username} wasn't resolved.
How can I resolve the ${username}?
Edit:
Note that the following runs fine:
site-deploy -Psonatype-oss-release -Dusername=puce
Edit 2:
As part of release:perform maven executes the following command:
/usr/share/maven/bin/mvn -s /tmp/release-settings7797889802430474959.xml deploy site-deploy --no-plugin-updates --batch-mode -Psonatype-oss-release -P nexus -f pom.xml
-Dusername=puce doesn't seem to get passed to this maven command...
Also note that help:effective-pom shows the following maven-release-plugin configuration:
<plugin>
<artifactId>maven-release-plugin</artifactId>
<version>2.2.2</version>
<configuration>
<mavenExecutorId>forked-path</mavenExecutorId>
<useReleaseProfile>false</useReleaseProfile>
<arguments>-Psonatype-oss-release</arguments>
</configuration>
</plugin>
So 'arguments' gets defined and its value seems to reach the embedded maven command instead of the value passed on the command line...

What I've successfully done in the past is as follows:
Define a property in the POM file, e.g.:
<properties>
<release.arguments></release.arguments>
</properties>
Add the POM property to the plugin configuration in the POM file, e.g.;
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<configuration>
<arguments>${release.arguments}</arguments>
...
Pass the argument through the property on the command-line, e.g.:
mvn release:prepare -Drelease.arguments="-N -Prelease"
Hope this helps.

Overriding
<plugin>
<artifactId>maven-release-plugin</artifactId>
<version>2.2.2</version>
<configuration>
<mavenExecutorId>forked-path</mavenExecutorId>
<useReleaseProfile>false</useReleaseProfile>
<arguments>-Psonatype-oss-release</arguments>
</configuration>
</plugin>
with
<plugin>
<artifactId>maven-release-plugin</artifactId>
<configuration>
<mavenExecutorId>forked-path</mavenExecutorId>
<useReleaseProfile>false</useReleaseProfile>
<arguments>-Psonatype-oss-release -Dusername=${username}</arguments>
</configuration>
</plugin>
in one of the parents did the trick.
It seems to be a bug that the value on the command line doesn't override the value in the POM.

My solution was similar to Sander Verhagen's. I added only line only though.
How I run:
mvn --batch-mode release:prepare -Denvironment=production
My config:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version>
<configuration>
<tag>${artifactId}-${version}-${environment}</tag>
<arguments>-Denvironment=${environment}</arguments>
<releaseProfiles>release</releaseProfiles>
</configuration>
</plugin>
</plugins>
</build>
The difference is that now my submodules use the variable environment and I don't need to define it twice (I.e. -Darguments=-Denvironment=production -Denvironment=production). I also gives me the flexibility of not adding the properties tag.

Related

Can't run java code when using package [duplicate]

Is there a Maven "phase" or "goal" to simply execute the main method of a Java class? I have a project that I'd like to test manually by simply doing something like "mvn run".
See the exec maven plugin. You can run Java classes using:
mvn exec:java -Dexec.mainClass="com.example.Main" [-Dexec.args="argument1"] ...
The invocation can be as simple as mvn exec:java if the plugin configuration is in your pom.xml. The plugin site on Mojohaus has a more detailed example.
<project>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>com.example.Main</mainClass>
<arguments>
<argument>argument1</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
</project>
1. Edit POM.xml
Add the following property in pom.xml. Make sure you use the fully qualified class name (i.e. with package name) which contains the main method:
<properties>
<exec.mainClass>fully-qualified-class-name</exec.mainClass>
</properties>
2. Run Command
Now from the terminal, trigger the following command:
mvn clean compile exec:java
NOTE You can pass further arguments via -Dexec.args="xxx" flag.
The above mentioned answers are correct but I am simplifying it for noobs like me.Go to your project's pom file. Add a new property exec.mainClass and give its value as the class which contains your main method. For me it was DriverClass in mainpkg. Change it as per your project.
Having done this navigate to the folder that contains your project's pom.xml and run this on the command prompt mvn exec:java. This should call the main method.
No need to add new plugin in pom.xml. Just run this command
mvn org.codehaus.mojo:exec-maven-plugin:1.5.0:java -Dexec.mainClass="com.example.Main" | grep -Ev '(^\[|Download\w+:)'
See the maven exec plugin for more usage.
Give the Exec Maven plugin a try
clean package exec:java -P Class_Containing_Main_Method command is also an option if you have only one Main method(PSVM) in the project, with the following Maven Setup.
Don't forget to mention the class in the <properties></properties> section of pom.xml :
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.main.class>com.test.service.MainTester</java.main.class>
</properties>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>${java.main.class}</mainClass>
</configuration>
</plugin>
STS Run Configuration along with above Maven Setup:

Maven install does not encode in UTF-8 even if configured

Hi I have a problem with the encoding of my project.
When I run JUnit tests from eclipse, there are no failures. The problem is when I do maven > clean maven > install, one of the tests fails.
I have this string: "ADMINISTRACIÓN", and it's fine when i run the JUnit from eclipse, but I've printed the variable and when maven does the tests, the value of this string is: "ADMINISTRACI�N".
I've changed every property I could find of encoding in eclipse to UTF-8.
-Configured the pom this way:
(...)
<project>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
(...)
</properties>
</project>
(...)
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
(...)
But the output is the same. I have a coworker that has the same project than me, and the same eclipse client and config, and her maven tests print accents with no trouble.
Any further ideas?
Thanks a lot!
Try run your build with:
mvn -DargLine=-Dfile.encoding=UTF-8 clean insall
if help, you can configure surefire in project:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-Dfile.encoding=${project.build.sourceEncoding}</argLine>
</configuration>
</plugin>
Problem may occur because System.out use default system encoding, you can change this be setting file.encoding java property.
I tried all settings mentioned in this post to build my project successfully however that didn't work for me. At last I was able to build my project successfully with mvn -DargLine=-Dfile.encoding=UTF-8 clean insall command.

How to specify JAR location of a Maven dependency as plugin parameter?

I've tried to follow the configuration from this response on SO to use the jar location in my local repo as a plugin parameter, but it doesn't seem to work. I don't know if this due to a newer Maven version than the response (I'm using Maven 3.2.5).
In my pom.xml, I need to add a javaagent to my surefire plugin definition. The javaagent jar file is a dependency in my project.
I have tried the following:
<dependencies>
<dependency>
<groupId>org.jmockit</groupId>
<artifactId>jmockit</artifactId>
<version>${jmockit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
...
<!-- Configuration to use jmockit on IBM J9 -->
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-javaagent:${org.jmockit:jmockit:jar}</argLine>
</configuration>
</plugin>
I was expecting that the ${org.jmockit:jmockit:jar} would be expanded to the location of the jar, but in my mvn console I see the following error:
[ERROR] Command wascmd.exe /X /C "C:\IBM\SDP\jdk\jre\bin\java -javaagent:${org.jmockit:jmockit:jar} -jar C:\dev\Eclipse\rtc-connector\target\surefire\surefirebooter1389906134960134.jar C:\dev\Eclipse\rtc-connector\target\surefire\surefire5488684370604495471tmp C:\dev\Eclipse\rtc-connector\target\surefire\surefire_05402037720997438783tmp"
So obviously the parameter is not getting expanded. I was hoping/expecting to see something like -javaagent:c:\users\eric\.m2\repository\org.jmockit\1.20\jmockit-1.20.jar or something similar.
Is there a clean way I can reference the jar from my dependency in my plugin configuration? I know I can use the dependency-plugin to copy the jar to a known location in my target folder and then point to that, but I was hoping there would be an easier solution that doesn't require the intermediary step.
To be variable expanded need to add maven-dependency-plugin:
<!-- obtain ${*:*:jar} properties -->
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>getClasspathFilenames</id>
<goals>
<goal>properties</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-javaagent:${org.jmockit:jmockit:jar}</argLine>
</configuration>
</plugin>
You need to set the "argLine" tag value as follow :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<configuration>
<argLine>
-javaagent:"${localRepository}/org/jmockit/jmockit/${jmockit.version}/jmockit-${jmockit.version}.jar"
</argLine>
</configuration>
</plugin>
You need to set the .m2 repo path where the jmockit jar is present. It's working for me.
The maven-dependency-plugin contains a goal build-classpath which would solve this problem.
On command line you can do things like this:
mvn dependency:build-classpath -DincludeArtifactIds=testng -DincludeGroupIds=testng
which results in:
C:\repository\org\testng\testng\6.8.21\testng-6.8.21.jar
The resulting classpath can also be put into a property outputProperty ....
This could be configured into pom as well...

Error in Maven building?

After i wrote
mvn -f pom.xml compile exec:java -Dexec.classpathScope=Compile-Dexec.main Class=storm.starter.WordCountTopology
and found this !!
[INFO] One or more required plugin parameters are invalid/missing for
'exec:java'
[0] Inside the definition for plugin 'exec-maven-plugin' specify the
following:
... VALUE
-OR-
on the command line, specify: '-Dstorm.topology=VALUE
If you link your pom.xml then this would be easier. I'm guessing you're using Storm. Have you written your own topologyClass? From the documentation:
topologyClass
The class name of the topology driver (e.g. "com.foo.bar.MyTopology")
Command line override: -Dmaven.storm.topology=
The documentation also gives you the code for your pom but you might want to add exec-maven-plugin to your pom.xml like so:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>com.domain.yourApp</mainClass>
</configuration>
</plugin>
</plugins>
</build>
One thing to note: you need to alter mainClass to match the class in your project that contains the main method you want to execute.
Then you can just run mvn exec:java.

Executing Maven3 exec plugin with no inheritance

I have a multi module maven java project where I want to execute the exec plugin in order to execute a custom command after the Jars has been created.
I call maven package assembly:assembly exec:exec on the parent POM to create the project output.
I used the following in the parent POM:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<configuration>
<executable>myExecutable.exe</executable>
<workingDirectory>${basedir}</workingDirectory>
<arguments>....</arguments>
</configuration>
</plugin>
When doing so, my executable got executed correctly but was executed for every child module as well.
In order to try and fix it, I've set the plugin to not get inherit to the child modules:
<inherited>false</inherited>
But now the exec plugin fails with a The parameters 'executable' for goal org.codehaus.mojo:exec-maven-plugin:1.2.1:exec are missing or invalid error.
I've tried setting the plugin to run under the "executions" element and assign it to a maven life cycle phase. This caused the executable to run successfully, but I am unable to do this since I need to execute other plugins (assembly plugin) before I execute this exec plugin.
How can I run this (exec) plugin just once after the package phase has completed and the other plugin (assembly) has been finished as well?
In other words, I want to execute 'package' for all of my child modules and then execute the exec plugin just once from the parent.
I will appreciate any help.
When you run a command at the parent POM level, you are telling Maven to invoke that part of the lifecycle for every module.
If you only want to invoke exec:exec for the child module, you should only declare that plugin for the child module (as it has no meaning for other modules that inherit the parent) and invoke Maven with the --projects or -pl argument:
mvn -pl child package assembly:assembly exec:exec
This command, when executed from the parent project, executes package assembly:assembly exec:exec for only the child project.
However, if what you really are trying to accomplish is that you can package all of the modules in a single command, and you want the assembly:assembly and exec:exec goals to be executed for that child module during the package phase, then you want to bind these plugins to that phase. For example:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<configuration>
<executable>myExecutable.exe</executable>
<workingDirectory>${basedir}</workingDirectory>
<arguments>....</arguments>
</configuration>
<executions>
<!-- run the exec goal of this plugin every time this module is packaged -->
<execution>
<phase>package</phase>
<goals><goal>exec</goal></goals>
</execution>
</executions>
</plugin>
This will skip inheritance on exec-maven-plugin executions previously configured:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<phase>none</phase>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
...
</build>

Categories

Resources