Maven - 配置 POM 以执行 JAR
Maven - Configuring POM to execute JAR
我有一个简单的 Hello World 应用程序 'App',它是在 Maven 项目中创建的。我知道在构建项目后,需要 Maven 程序集插件才能执行构建项目时创建的 JAR。我已按照在以下位置找到的说明进行操作:
http://maven.apache.org/plugins/maven-assembly-plugin/usage.html
但是在编辑我的 pom.xml 并重新构建项目后,我仍然无法 运行 我的 JAR 文件。
这是我的 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>
我的 POM 中是否有错误或我遗漏的其他行导致我无法 运行 生成的 JAR 文件?
编辑:似乎 JAR 没有清单属性(见评论)。我想我可以使用 7zip 或类似程序打开 jar 并手动添加清单文件,但真正的问题是如何首先使用 Maven 创建 JAR 以包含清单文件。
您看错了 JAR。上面的 POM 将创建至少两个 JAR:
mini-project-6-ex6-1.0-SNAPSHOT.jar
mini-project-6-ex6-1.0-SNAPSHOT-jar-with-dependencies.jar
前者只是模块的代码,后者是代码加上所有的依赖
如果您 运行 mvn install
,您将只会得到第一个,因为 Assembly 插件没有自动配置为 运行。您可以 运行 手动使用 mvn assembly:assembly
。
如果你想让它在你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>
见http://maven.apache.org/plugins/maven-assembly-plugin/usage.html#Execution:_Building_an_Assembly
我有一个简单的 Hello World 应用程序 'App',它是在 Maven 项目中创建的。我知道在构建项目后,需要 Maven 程序集插件才能执行构建项目时创建的 JAR。我已按照在以下位置找到的说明进行操作:
http://maven.apache.org/plugins/maven-assembly-plugin/usage.html
但是在编辑我的 pom.xml 并重新构建项目后,我仍然无法 运行 我的 JAR 文件。
这是我的 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>
我的 POM 中是否有错误或我遗漏的其他行导致我无法 运行 生成的 JAR 文件?
编辑:似乎 JAR 没有清单属性(见评论)。我想我可以使用 7zip 或类似程序打开 jar 并手动添加清单文件,但真正的问题是如何首先使用 Maven 创建 JAR 以包含清单文件。
您看错了 JAR。上面的 POM 将创建至少两个 JAR:
mini-project-6-ex6-1.0-SNAPSHOT.jar
mini-project-6-ex6-1.0-SNAPSHOT-jar-with-dependencies.jar
前者只是模块的代码,后者是代码加上所有的依赖
如果您 运行 mvn install
,您将只会得到第一个,因为 Assembly 插件没有自动配置为 运行。您可以 运行 手动使用 mvn assembly:assembly
。
如果你想让它在你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>
见http://maven.apache.org/plugins/maven-assembly-plugin/usage.html#Execution:_Building_an_Assembly