I want to execute a jar file with parameters from maven. The command I want to execute is listed below. I have the perf4j jar file in the dependency. The times.log file is in they filesystem.
java -jar perf4j-0.9.16.jar times.log
Thanks
You might want to take a look # exec-maven-plugin
first
mvn clean install
than
mvn exec:java -Dexec.mainClass="com.java.App" -Dexec.args="Args"
What do you really want to do ? Using a jar (which is a dependency) to monitor your app ?
Did you took a look at maven exec plugin ?
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
...
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>maven</executable>
<!-- optional -->
<workingDirectory>/tmp</workingDirectory>
<arguments>
<argument>-X</argument>
<argument>myproject:dist</argument>
...
</arguments>
</configuration>
</plugin>
</plugins>
</build>
...
</project>
I had looked at maven exec plugin but wasn't sure how and where to specify the jar file name, hence thanks for your responses, but I was looking at a little more info especially with jar file. With some trial and error the following worked. I had to find the main class to use from the jar file. mvn install runs the file and produces the following output:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>org.perf4j.LogParser</mainClass>
<arguments>
<argument>InlineLogging.log</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.perf4j</groupId>
<artifactId>perf4j</artifactId>
</dependency>
</dependencies>
Related
I am from ANT background and newbie to Maven.
For some reason, I need to execute shell script before maven tries to fetch snapshot dependencies.
So I wrote following plugin configuration, but I not getting how can I make it invoke before resolving dependencies task.
I am using Apache Maven 3.0.5
Following is the part of my pom.xml
<build>
<finalName>edte</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.5.0</version>
<executions>
<execution>
<phase>clean</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>Docs/ci/delete_snapshots.sh</executable>
<arguments>
<argument>${user.home}</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
Any help is appreciated.
I have a windows batch file to create me a file myUser.aaa.
And I call this bat file using exec-maven-plugin
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<phase>pre-integration-test</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>scripts/MyBat.bat</executable>
</configuration>
</plugin>
</plugins>
What I want to know is how can I install the file to my repo after the MyBat.bat was executed?
I first wanted to use an mvn command from the bat file to upload it but this job gets executed from a Jenkins server and it has its own maven config. If I run mvn from the bat file it will refer to the maven on the local system.
I would suggest to use the build-helper-maven-plugin to add the supplemental artifact to your build and afterwards it will be deployed in one go with the rest which can be done like this:
<project>
...
<build>
<plugins>
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.9.1</version>
<executions>
<execution>
<id>attach-artifacts</id>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>some file</file>
<type>extension of your file </type>
<classifier>optional</classifier>
</artifact>
...
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
May be you should bind the exec-maven-plugin to an earlier phase or the build-helper-maven-plugin to a later phase. I would suggest to use prepare-package for the exec-maven-plugin. Furthermore i would suggest to use uptodate versions of the plugins.
I am using findbugs-maven-plugin in the verify phase of the maven life cycle. i.e. it runs on mvn clean install. This is the code I have in my parent pom.xml (in a multi-module project).
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>findbugs</id>
<phase>verify</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<configuration>
<findbugsXmlOutputDirectory>target/findbugs</findbugsXmlOutputDirectory>
<failOnError>false</failOnError>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>xml-maven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>transform</goal>
</goals>
</execution>
</executions>
<configuration>
<transformationSets>
<transformationSet>
<dir>target/findbugs</dir>
<outputDir>target/findbugs</outputDir>
<stylesheet>plain.xsl</stylesheet>
<fileMappers>
<fileMapper implementation="org.codehaus.plexus.components.io.filemappers.FileExtensionMapper">
<targetExtension>.html</targetExtension>
</fileMapper>
</fileMappers>
</transformationSet>
</transformationSets>
</configuration>
<dependencies>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>findbugs</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
</plugin>
This is working fine and html files are being generated in each module target. However I want to take this a step further by being able to use parameters allowed by findbugs during the maven build (for example onlyAnalyze). I do not want to add configuration in the pom.xml.
I want the build process to remain the same unless I specify by some command that I want to analyze only one class, for example by running:
mvn clean install -Dfindbugs:onlyAnalyze=MyClass
Do you know of a way I can do this?
This is how you can call a standalone goal:
plugin-prefix:goal or groupId:artifactId:version:goal to ensure the right version.
In your case: findbugs:findbugs
With -Dkey=value you can set plugin parameters if they are exposed. http://mojo.codehaus.org/findbugs-maven-plugin/findbugs-mojo.html doesn't show that option. Just to compare: http://mojo.codehaus.org/findbugs-maven-plugin/help-mojo.html does have such options. Here it is still called Expression with ${key}, nowadays it's generated as User property with just key.
If you want onlyAnalyze to be set from commandline, either ask the mojo-team to fix that, or do the following:
<project>
<properties>
<findbugs.onlyAnalyze>false</findbugs.onlyAnalyze> <!-- default value -->
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<configuration>
<onlyAnalyze>${findbugs.onlyAnalyze}</onlyAnalyze>
</configuration>
</plugins>
</build>
</project>
Now you can call mvn findbugs:findbugs -Dfindbugs.onlyAnalyze=true
I am trying to set up my POM such that when I do mvn exec:exec or mvn exec:java it will first compile the source and iff successful, execute it.
I have the following and have tried moving the <execution> part about but can't get it to work:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
<executions>
<execution>
<phase>exec</phase>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>my.main.class</mainClass>
</configuration>
</plugin>
</plugins>
</build>
When I do either mvn exec:exec ... or mvn exec:java it doesn't first compile. I have tried putting the <execution> part in the exec plugin section but that didn't work either?
It's an old topic, but someone else might be interested in an alternative solution for this.
It's not exactly what you were looking for, but you can compile and execute using a single command:
mvn compile exec:exec
This way Maven will always compile the project before executing it.
You can bind the exec plugin to a phase following compile in build lifecycle (verify in the example below):
<profile>
<id>proxy</id>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<mainClass>my.main.class</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
and than run mvn verify.
I see the answer is very late and you may have found a solution.
I'm just leaving as a reference for others who may need it.
This will not quite answer the question but it will help us to accomplish the main objective you have.
Instead of executing a build goal when running the mvn exec:java we can execute the mvn exec:java when calling a 'post-build" phase/goal.
This will effectively make that phase unusable as it was because we will be substituting it by the execution but if we have a "post-build" goal that we are not using (like package or install) we can use it for the execution like so:
using package goal will end up like:
...
<properties>
...
<main.class>${project.groupId}.MainClass</main.class>
...
</properties>
...
<build>
<plugins>
...
<!-- region Forces the app to be executed when using mvn package -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<mainClass>${main.class}</mainClass>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- endregion -->
...
</plugins>
</build>
This way when we execute goal package with: mvn package the plugin will be executed and the application runned.
I have my code packaged into a jar
The jar is packaged ok.
jar -tfv target/test-1.0-SNAPSHOT.jar
com/
com/codevalid/
com/codevalid/App.class
log4j.xml
META-INF/maven/com.codevalid/test/pom.xml
META-INF/maven/com.codevalid/test/pom.properties
I can execute them when they are present as individual class files using exec:java
How to run class file within jar using maven exec:java?
Ok, this is what i finally ended up doing.
I built the jar using
mvn assembly:single
and used
java -jar ./target/App-1.0-SNAPSHOT-jar-with-dependencies.jar com.codevalid.App
I did see an alternative where i could have used
mvn exec:java -Dexec.mainClass="com.codevalid.App"
But i was not sure how pass the name of the jar as a classpath
You can run a jar file using the exec:java goal by adding some arguments:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<mainClass>org.example.Main</mainClass>
<arguments>
<argument>-jar</argument>
<argument>target/myJar-1.0-SNAPSHOT.jar</argument>
</arguments>
</configuration>
</plugin>
If you have an executable jar and don't want to define the entry point, you need to set the executable and use the exec:exec goal:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<executable>java</executable>
<arguments>
<argument>-jar</argument>
<argument>target/myJar-1.0-SNAPSHOT.jar</argument>
</arguments>
</configuration>
</plugin>
You need to include your jar file as a dependency to the exec plugin, e.g. like this:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>com.codevalid.App</mainClass>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>myGroup</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
You can skip the dependency declaration if the com.codevalid.App class is compiled as part of your current project.
You have to specify classpathScope and includePluginDependencies or includeProjectDependencies parameters to pickup jar files on the classpath.
Here is an example:
<configuration>
<executable>java</executable>
<mainClass>com.google.jstestdriver.JsTestDriver</mainClass>
<classpathScope>test</classpathScope>
<includePluginDependencies>true</includePluginDependencies>
<includeProjectDependencies>true</includeProjectDependencies>
<commandlineArgs>--port 9876</commandlineArgs>
</configuration>