如何告诉Maven生成可执行jar [英] How to tell Maven to build an executable jar

查看:98
本文介绍了如何告诉Maven生成可执行jar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎应该很简单,但是我还没有弄清楚,并且从阅读文档和搜索网络中没有得到太多帮助.为了学习在多模块项目中使用Maven(2.2.1),我使用Maven创建了一个具有类C(包含main)和子类S的简单项目P.运行mvn install时,构建运行并生成BUILD SUCCESSFUL消息.但是,当我运行生成的C jar时,出现异常:线程"main"中的异常java.lang.NoClassDefFoundError:P/S"

This seems like it should be simple, but I've not figured it out and haven't gotten much help from reading the docs and searching the web. In an effort to learn to use Maven (2.2.1) with a multimodule project, I've used Maven to create a simple project P with a class C (containing main) and subclass S. When I run mvn install the build runs and produces a BUILD SUCCESSFUL message. However, when I run the resulting C jar, I get an exception: "Exception in thread "main" java.lang.NoClassDefFoundError: P/S"

目录结构(不显示src等子目录)为:

The directory structure (not showing the src, etc. subdirectories) is:

P
| --C
| --S

P
|--C
|--S

目录P中的pom.xml当前为:

The pom.xml in directory P currently is:

<?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是:

The pom.xml in C is:

<?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是:

and the pom.xml in S is:

<?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>

以上是经过几次尝试的所有最新版本.

The above are all the latest versions after several attempts.

对于此简单的学习练习,Java类为:

For this simple learning exercise, the Java classes are:

P/C/src/main/java/P/C.java:

P/C/src/main/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:

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

package P;

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

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

So, how do I tell Maven to include the S class in the C jar?

推荐答案

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

After more research, I've found a means of telling Maven to build a jar file which includes all the needed classes. The only pom I had to change was in the P directory. First, the following section had to be removed from the build-plugins section:

    <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>

这篇关于如何告诉Maven生成可执行jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆