gpt4 book ai didi

java - Maven - 配置 POM 来执行 JAR

转载 作者:行者123 更新时间:2023-11-30 08:12:56 25 4
gpt4 key购买 nike

我有一个简单的 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 似乎没有 list 属性(请参阅评论)。我想我可以使用 7zip 或类似程序打开 jar 并手动添加 list 文件,但真正的问题是如何使用 Maven 创建 JAR 以首先包含 list 文件。

最佳答案

您正在查看错误的 JAR。上面的 POM 将创建至少两个 JAR:

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

前者只是模块的代码,后者是代码加上所有依赖项。

如果您运行 mvn install,您只会获得第一个,因为程序集插件未配置为自动运行。您可以使用 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

关于java - Maven - 配置 POM 来执行 JAR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30104004/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com