如何告诉Maven构建一个可执行jar

时间:2011-02-24 17:10:37

标签: java maven-2 packages

这看起来应该很简单,但我没有想到它,并且在阅读文档和搜索网页时没有得到太多帮助。为了学习使用Maven(2.2.1)和多模块项目,我使用Maven创建了一个带有C类(包含main)和子类S的简单项目P.当我运行mvn install时构建运行并生成BUILD SUCCESSFUL消息。但是,当我运行生成的C jar时,我得到一个异常:“线程中的异常”主“java.lang.NoClassDefFoundError:P / S”

目录结构(未显示src等子目录)是:


 | --C
 | --S

目录P中的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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>P</groupId>
  <artifactId>P</artifactId>
  <packaging>pom</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>P</name>
  <url>http://maven.apache.org</url>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>P</groupId>
        <artifactId>C</artifactId>
        <version>${project.version}</version>
        <scope>compile</scope>
      </dependency>
      <dependency>
        <groupId>P</groupId>
        <artifactId>S</artifactId>
        <version>${project.version}</version>  
        <scope>compile</scope>
      </dependency>
      <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
          <archive>
            <manifest>
              <mainClass>P.C</mainClass>
              <packageName>P.C</packageName>
            </manifest>
          </archive>
        </configuration>
      </plugin>
    </plugins>
  </build>

  <modules>
    <module>C</module>
    <module>S</module>
  </modules>

</project>

C中的pom.xml是:

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <artifactId>P</artifactId>
    <groupId>P</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <groupId>P</groupId>
  <artifactId>C</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>C</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>P</groupId>
      <artifactId>S</artifactId>
      <version>${project.version}</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

并且S中的pom.xml是:

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <artifactId>P</artifactId>
    <groupId>P</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <groupId>P</groupId>
  <artifactId>S</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>S</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

以上是经过多次尝试后的所有最新版本。

对于这个简单的学习练习,Java类是:

P / C / SRC /主/ JAVA / P / C.java:

package P;

public class C
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
        S o = new S();
        o.sayHey();
    }
}

和S / src / main / java / P / S.java:

package P;

public class S
{
    public void sayHey()
    {
        System.out.println("Hey!");
    }
}

那么,我如何告诉Maven在C jar中包含S类?

3 个答案:

答案 0 :(得分:3)

Maven默认情况下不包含工件中的依赖项:要运行P,您仍然需要在类路径中使用S.

这通常不是问题,因为maven本身使用传递依赖:例如,如果你有另一个项目依赖于P,它会自动将P和S都放在你的类路径中。

如果你想创建一个'uber-jar':一个包含所有依赖项的自包含jar,请查看'shade'插件:http://maven.apache.org/plugins/maven-shade-plugin/

答案 1 :(得分:3)

经过更多的研究,我发现了一种告诉Maven构建一个包含所有必需类的jar文件的方法。我必须更改的唯一pom是在P目录中。首先,以下部分必须从build-plugins部分 删除

    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
      <archive>
        <manifest>
          <mainClass>P.C</mainClass>
          <packageName>P.C</packageName>
        </manifest>
      </archive>
    </configuration>

取而代之的是我:

    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
      <execution>
        <id>create-executable-jar</id>
        <phase>package</phase>
        <goals>
          <goal>single</goal>
        </goals>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
          <archive>
            <manifest>
              <mainClass>P.C</mainClass>
              <packageName>P.C</packageName>
            </manifest>
          </archive>
        </configuration>
      </execution>
    </executions>

答案 2 :(得分:1)

maven exec插件可能会解决您的问题。您可以使用它来运行C.jar,它将管理类路径(并确保包含S.jar)。

在P pom.xml中添加与此类似的内容:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.1</version>
        <executions>
          <execution>
            ...
            <goals>
              <goal>java</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <mainClass>P.C</mainClass>
        </configuration>
      </plugin>
    </plugins>
  </build>
   ...
</project>

然后你应该能够运行这样的程序:

mvn exec:java -Dexec.mainClass="P.C"