我正在尝试创建一个基于maven的项目,该项目有一个父pom,然后是两个模块。一个用于业务,一个用于依赖于业务的命令行应用程序。
父pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>dk.fitfit.triangle.parent</groupId>
<artifactId>triangle-parent</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>Business</module>
<module>Cmd</module>
</modules>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>dk.fitfit.triangle.cmd.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
然后我有业务pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>triangle-parent</artifactId>
<groupId>dk.fitfit.triangle.parent</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>dk.fitfit.triangle.business</groupId>
<artifactId>triangle-business</artifactId>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
最后是命令行应用程序的pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>triangle-parent</artifactId>
<groupId>dk.fitfit.triangle.parent</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>dk.fitfit.triangle.cmd</groupId>
<artifactId>triangle-cmd</artifactId>
<dependencies>
<dependency>
<groupId>dk.fitfit.triangle.business</groupId>
<artifactId>triangle-business</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
问题是当我尝试运行我的jar文件时,我收到以下错误消息
Exception in thread "main" java.lang.NoClassDefFoundError: dk/fitfit/triangle/business/Triangle
at dk.fitfit.triangle.cmd.Main.main(Main.java:17)
Caused by: java.lang.ClassNotFoundException: dk.fitfit.triangle.business.Triangle
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
我正在尝试使用以下命令运行我的jar
mvn install && mvn package && java -jar Cmd/target/triangle-cmd-1.0-SNAPSHOT.jar 1 2 3
关于我做错了什么的线索?
答案 0 :(得分:8)
在命令行应用程序的pom中,将maven-jar-plugin替换为以下内容。
<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>dk.fitfit.triangle.cmd.Main</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
现在你将得到一个包含所有可以使用的依赖项的胖jar。
答案 1 :(得分:3)
每个模块都会生成一个单独的jar,也就是说,你有一个商业jar和一个cmd-jar。运行java命令时,只指定cmd-jar。
现在您有两个选择: