简体   繁体   English

Maven-配置POM执行JAR

[英]Maven - Configuring POM to execute JAR

I have a simple Hello World application 'App' which has been made in a Maven project. 我有一个简单的Hello World应用程序“ App”,它是在Maven项目中制作的。 I'm aware that after building the project, the Maven assembly plugin is required in order to execute the JAR which is created when the project is built. 我知道在构建项目之后,需要Maven程序集插件才能执行在构建项目时创建的JAR。 I have followed the instructions found at: 我已按照以下说明进行操作:

http://maven.apache.org/plugins/maven-assembly-plugin/usage.html http://maven.apache.org/plugins/maven-assembly-plugin/usage.html

But after editing my pom.xml and re-building the project, I still cannot run my JAR file. 但是,在编辑pom.xml并重新构建项目之后,我仍然无法运行JAR文件。

Here is my pom.xml: 这是我的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>com.mycompany</groupId>
<artifactId>mini-project-6-ex6</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
</properties>
<build>
<plugins>
  <plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.5.4</version>
    <configuration>
      <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
      <archive>
        <manifest>
          <mainClass>com.mycompany.mini.project.ex6.App</mainClass>
        </manifest>
      </archive>
    </configuration>
  </plugin>
</plugins>
</build>

Is there a mistake in my POM or additional lines which I have left out which is causing me to not be able to run the resulting JAR file? 我的POM或我遗漏的其他行是否有错误,导致我无法运行生成的JAR文件?

EDIT: It seems as though the JAR has no manifest attribute (see comments). 编辑:好像JAR没有清单属性(请参阅注释)。 I suppose that I could open the jar using 7zip or similar program and manually add a manifest file, but the real question is how to create the JAR using Maven to include a manifest file in the first place. 我想我可以使用7zip或类似程序打开jar并手动添加清单文件,但是真正的问题是如何使用Maven创建JAR以首先包含清单文件。

You're looking at the wrong JAR. 您正在查看错误的JAR。 The POM above will create at least two JARs: 上面的POM将创建至少两个JAR:

mini-project-6-ex6-1.0-SNAPSHOT.jar
mini-project-6-ex6-1.0-SNAPSHOT-jar-with-dependencies.jar

The former is only the code of the module, the latter is the code plus all the dependencies. 前者只是模块的代码,后者是代码加上所有依赖项。

If you run mvn install , you will only get the first one since the Assembly Plugin isn't configured to run automatically. 如果运行mvn install ,则只有第一个mvn install ,因为未将Assembly Plugin配置为自动运行。 You can run it manually with mvn assembly:assembly . 您可以使用mvn assembly:assembly手动运行它。

If you want it to run automatically when you mvn install , you need to change the POM: 如果要在mvn install时自动运行,则需要更改POM:

<plugins>
  <plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.5.4</version>
    <configuration>
      ...
    </configuration>
    <executions>
      <execution>
        <id>make-assembly</id> <!-- this is used for inheritance merges -->
        <phase>package</phase> <!-- bind to the packaging phase -->
        <goals>
          <goal>single</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

See http://maven.apache.org/plugins/maven-assembly-plugin/usage.html#Execution:_Building_an_Assembly 参见http://maven.apache.org/plugins/maven-assembly-plugin/usage.html#Execution:_Building_an_Assembly

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM