I have a Maven project that I need somehow to compile so I'd be able to transfer the compiled file to a different machine and then execute there (with potentially different Java version). I looked at this SO question
and was able to execute:
mvn clean install
mvn dependency:copy-dependencies
Yet when I tried:
cd target/
java -cp myApp-0.0.1.jar:dependency myApp
I got
Error: Could not find or load main class myApp
My pom.xml does not have mvn-assembly-plugin or maven-jar-plugin (and I successfully get the .jar in target/ after mvn clean install).
I am not sure whether those steps are related to my goal because what I need is to create something that will run without any java -jar or mvn and could be ported and run on a different machine.
There is Maven build plugin that would bundle all your dependencies in a JAR. You could execute it simply with
java -jar mybundled.jar
without caring about class path dependencies.
And here comes the Maven
plugin. Replace the MainClass
with your entry point.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.yourdomain.Main</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
As it is bound to the package lifecycle
, you would find your bundled JAR in your target directory after doing a ...
mvn package
Regarding your concern that your code won't run on another machine, Java is platform-independent and generally you can rely on that. However, three things come to my mind that can make life hard:
ps
won't work on windows and dir /s
probably not on *nix. source
and target
compile options, too: http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html#crosscomp-example
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.