Maven exec: java runs class file inside jar

I have a code packed in a jar

The jar is packed normally.

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 presented as separate class files using exec:java

How to run class file in jar using maven exec:java ?

+6
source share
4 answers

Well, this is what I finally finished doing.
I built a jar using

 mvn assembly:single 

and used

 java -jar ./target/App-1.0-SNAPSHOT-jar-with-dependencies.jar com.codevalid.App 

I saw an alternative where I could use

 mvn exec:java -Dexec.mainClass="com.codevalid.App" 

But I was not sure how to pass the jar name as a classpath

+5
source

You need to include your jar file as a dependency on the exec plugin, for example. eg:

 <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 com.codevalid.App compiled as part of your current project.

+3
source

You must specify the classpathScope and includePluginDependencies or includeProjectDependencies parameters to receive jar files in 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> 
+1
source

You can run the jar file using the exec:java target by adding a few 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 you do not want to define an entry point, you need to set executable and use the exec:exec target:

 <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> 
+1
source

Source: https://habr.com/ru/post/911935/


All Articles