首页 文章

用maven Build 可执行jar?

提问于
浏览
116

我正在尝试使用maven为一个名为“logmanager”的小型家庭项目生成一个可执行jar,就像这样:

How can I create an executable JAR with dependencies using Maven?

我将那里显示的代码段添加到pom.xml中,然后运行mvn assembly:assembly . 它在logmanager / target中生成两个jar文件:logmanager-0.1.0.jar和logmanager-0.1.0-jar-with-dependencies.jar . 当我双击第一个jar时出现错误:

Could not find the main class: com.gorkwobble.logmanager.LogManager. Program will exit.

当我双击jar-with-dependencies.jar时出现稍微不同的错误:

Failed to load Main-Class manifest attribute from: C:\EclipseProjects\logmanager\target\logmanager-0.1.0-jar-with-dependencies.jar

我复制并粘贴了路径和类名,并检查了POM中的拼写 . 我的主要类从eclipse启动配置启动 . 有人可以帮我弄清楚为什么我的jar文件不会运行?另外,为什么有两个 jar 开始?如果您需要更多信息,请与我们联系 .

这是完整的 pom.xml ,供参考:

<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>com.gorkwobble</groupId>
  <artifactId>logmanager</artifactId>
  <name>LogManager</name>
  <version>0.1.0</version>
  <description>Systematically renames specified log files on a scheduled basis. Designed to help manage MUSHClient logging and prevent long, continuous log files.</description>
  <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.2</version>
            <!-- nothing here -->
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.2-beta-4</version>
            <configuration>
              <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
              </descriptorRefs>
              <archive>
                <manifest>
                  <mainClass>com.gorkwobble.logmanager.LogManager</mainClass>
                </manifest>
              </archive>
            </configuration>
            <executions>
              <execution>
                <phase>package</phase>
                <goals>
                  <goal>single</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
              <source>1.6</source>
              <target>1.6</target>
            </configuration>
          </plugin>
    </plugins>
  </build>
  <dependencies>
    <!-- commons-lang -->
    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.4</version>
    </dependency> 

    <!-- Quartz scheduler -->
    <dependency>
        <groupId>opensymphony</groupId>
        <artifactId>quartz</artifactId>
        <version>1.6.3</version>
    </dependency>
    <!-- Quartz 1.6.0 depends on commons collections -->
    <dependency>
      <groupId>commons-collections</groupId>
      <artifactId>commons-collections</artifactId>
      <version>3.1</version>
    </dependency>
    <!-- Quartz 1.6.0 depends on commons logging -->
    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.1</version>
    </dependency>
    <!-- Quartz 1.6.0 requires JTA in non J2EE environments -->
    <dependency>
      <groupId>javax.transaction</groupId>
      <artifactId>jta</artifactId>
      <version>1.1</version>
      <scope>runtime</scope>
    </dependency>

    <!-- junitx test assertions -->
    <dependency>
        <groupId>junit-addons</groupId>
        <artifactId>junit-addons</artifactId>
        <version>1.4</version>
        <scope>test</scope>
    </dependency>

    <!-- junit dependency; FIXME: make this a separate POM -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.1</version>
    </dependency>

  </dependencies>
  <dependencyManagement>
  </dependencyManagement>
</project>

4 回答

  • 238

    实际上,我认为你提到的question给出的答案只是 wrongUPDATE - 20101106: 有人修了它,这个答案是指version preceding the edit),这至少部分地解释了为什么你遇到麻烦 .


    它在logmanager / target中生成两个jar文件:logmanager-0.1.0.jar和logmanager-0.1.0-jar-with-dependencies.jar .

    第一个是在 package 阶段由 jar:jar 生成的logmanager模块的JAR(因为该模块具有 jar 类型的包装) . 第二个是 assembly:assembly 生成的程序集,应该包含当前模块及其依赖项中的类(如果使用了描述符 jar-with-dependencies ) .

    当我双击第一个jar时出现错误:找不到主类:com.gorkwobble.logmanager.LogManager . 程序将会退出 .

    如果您应用了作为参考发布的链接的建议配置,那么您配置了jar插件以生成可执行工件,如下所示:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <mainClass>com.gorkwobble.logmanager.LogManager</mainClass>
            </manifest>
          </archive>
        </configuration>
      </plugin>
    

    所以 logmanager-0.1.0.jar 确实是可执行的但是1.这不是你想要的(因为它不包含 com.gorkwobble.logmanager.LogManager (这就是错误所说的,检查jar的内容) .

    当我双击jar-with-dependencies.jar时出现稍微不同的错误:无法从以下位置加载Main-Class清单属性:C:\ EclipseProjects \ logmanager \ target \ logmanager-0.1.0-jar-with-dependencies . jar

    同样,如果您按照建议配置了程序集插件,那么您可以这样:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
      </plugin>
    

    使用此设置, logmanager-0.1.0-jar-with-dependencies.jar 包含当前模块 and 其依赖项中的类,但根据错误,其 META-INF/MANIFEST.MF doesn't 包含 Main-Class 条目(它可能与logmanager-0.1.0.jar中的MANIFEST.MF不同) . jar实际上是 not 可执行文件,这也不是你想要的 .


    所以,我的建议是从maven-jar-plugin中删除 configuration 元素,并像这样配置maven-assembly-plugin:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.2</version>
        <!-- nothing here -->
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.2-beta-4</version>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
          <archive>
            <manifest>
              <mainClass>org.sample.App</mainClass>
            </manifest>
          </archive>
        </configuration>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    

    当然,将 org.sample.App 替换为您要执行的类 . 小小的奖励,我将 assembly:single 绑定到了 package 阶段,因此您不必再运行 assembly:assembly 了 . 只需运行 mvn install ,程序集将在标准构建期间生成 .

    因此,请使用上面给出的配置更新您的pom.xml并运行 mvn clean install . 然后,cd进入 target 目录并再试一次:

    java -jar logmanager-0.1.0-jar-with-dependencies.jar
    

    如果您收到错误,请用它更新您的问题,并发布 META-INF/MANIFEST.MF 文件的内容和 pom.xml 的相关部分(插件配置部分) . 还请发布以下结果:

    java -cp logmanager-0.1.0-jar-with-dependencies.jar com.gorkwobble.logmanager.LogManager
    

    证明它在命令行上运行正常(无论eclipse在说什么) .

    编辑:对于Java 6,您需要配置maven-compiler-plugin . 将其添加到您的pom.xml:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
    
  • 14

    Pascal Thivent的答案也帮助了我 . But 如果您在 <pluginManagement> 元素中管理插件,则必须在插件管理之外再次定义程序集,否则如果运行 mvn install ,则依赖项不会打包在jar中 .

    <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>
        <version>1.0.0-SNAPSHOT</version>
        <packaging>jar</packaging>
    
    
        <build>
            <pluginManagement>
                <plugins>
    
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <version>3.1</version>
                        <configuration>
                            <source>1.6</source>
                            <target>1.6</target>
                        </configuration>
                    </plugin>
    
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-assembly-plugin</artifactId>
                        <version>2.4</version>
                        <configuration>
                            <archive>
                                <manifest>
                                    <mainClass>main.App</mainClass>
                                </manifest>
                            </archive>
                            <descriptorRefs>
                                <descriptorRef>jar-with-dependencies</descriptorRef>
                            </descriptorRefs>
                        </configuration>
                        <executions>
                            <execution>
                                <id>make-assembly</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>single</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
    
                </plugins>
    
            </pluginManagement>
    
            <plugins> <!-- did NOT work without this  -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-assembly-plugin</artifactId>
                </plugin>
            </plugins>
    
        </build>
    
    
        <dependencies>
           <!--  dependencies commented out to shorten example -->
        </dependencies>
    
    </project>
    
  • 5

    如果您不希望在程序包上执行程序集目标,则可以使用next命令:

    mvn package assembly:single
    

    这里 package 是关键字 .

  • 0

    右键单击该项目并提供maven build,maven clean,maven generate resource和maven install.jar文件将自动生成 .

相关问题