Maven jar 不包括清单中的 main
maven jar not including main in manifest
我不得不重新启动大约一年半前运行的旧项目,但现在当我这样做时:
mvn clean install
无论是在命令行上还是通过 Eclipse,它都可以正常编译,但不会在清单中添加 main-class AND 我确实有正确的指令。
我正在使用:
- Apache Maven 3.5.4
- JDK 10.0.2
- eclipse 4.8.0(光子)
所以这是 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.a.b.c</groupId>
<artifactId>JarNameHere</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<log4j.version>2.4</log4j.version>
<http.client.version>4.5.2</http.client.version>
<maven.compiler.source>10</maven.compiler.source>
<maven.compiler.target>10</maven.compiler.target>
</properties>
<dependencies>
<!-- DEPENDENCIES HERE, BUT REMOVED TO MAKE MORE READABLE -->
...
</dependencies>
<build>
<pluginManagement>
<plugins>
<!-- Maven Assembly Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<!-- get all project dependencies -->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!-- MainClass in mainfest make a executable jar -->
<archive>
<manifest>
<mainClass>com.a.b.c.MainClass</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- AspectJ configuration -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.11</version>
<configuration>
<complianceLevel>1.11</complianceLevel>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- -->
</plugins>
</pluginManagement>
</build>
</project>
您犯了一个 class 初学者的错误:您假设 pluginManagement
部分中配置的插件将在构建期间自动使用。事实上,他们不会。 AspectJ Maven 和 Assembly 都不会 运行 像这样,所以当然在你的 target
目录中不会有任何具有依赖项的 JAR。
您也需要将插件显式添加到 plugins
部分,至少通过组 ID 和名称引用它们(那里不需要版本或配置,除非您想覆盖您已经定义的内容pluginManagement
).
除此之外,您会注意到 AspectJ Maven 仍然配置错误,一旦插件被激活,构建就会失败。但这超出了你的问题范围,所以我不打算在这里详细说明。
P.S.: 我将您的设置复制到我自己的 POM 中,修复了它们并且可以确认只要 AspectJ 插件具有正确的设置,Assembly 插件就会按预期工作,包括清单右主 class.
更新:所以你只提供了一个没有任何代码编译的POM片段运行,但你想看我的完整POM。我觉得这有点奇怪,因为实际上你应该提供一个 MCVE。但无论如何,这就是我所拥有的:我只是将你的 Assembly 插件合并到我自己的一个项目中,我通常使用一个 One-JAR 插件(构建一个可执行的 JAR 的 JAR),用你的替换我的插件并检查我是否可以 运行 可执行文件 java -jar ...
。测试成功。不过,我仍然在 JDK 8,对于这个问题,我没有升级我的整个构建系统。对于示例,我还删除了除 AspectJ 运行time.
之外的所有其他依赖项
<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.a.b.c</groupId>
<artifactId>JarNameHere</artifactId>
<version>0.1-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<main-class>com.a.b.c.MainClass</main-class>
<aspectj.version>1.8.13</aspectj.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<pluginManagement>
<plugins>
<!-- Maven Assembly Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<!-- get all project dependencies -->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!-- MainClass in mainfest make a executable jar -->
<archive>
<manifest>
<mainClass>${main-class}</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- AspectJ configuration -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.11</version>
<configuration>
<!--<showWeaveInfo>true</showWeaveInfo>-->
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<Xlint>ignore</Xlint>
<complianceLevel>${maven.compiler.target}</complianceLevel>
<encoding>${project.build.sourceEncoding}</encoding>
<!--<verbose>true</verbose>-->
<!--<warn>constructorName,packageDefaultMethod,deprecation,maskedCatchBlocks,unusedLocals,unusedArguments,unusedImport</warn>-->
</configuration>
<executions>
<execution>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
</dependency>
</dependencies>
</project>
我毫不怀疑@kriegaex 给出的答案会在 Java 9 之前起作用,因为我的老 pom.xml 在 Java 8 上工作了很多年。事实证明我在问题中没有提供足够的信息,无法正确回答这个问题。我从 Java 8 升级到 Java 10 搞砸了 AspectJ 集成,所以它会在 Maven 开始创建 jar 之前失败。
注意:我会继续完善它,因为使用 pluginManagement 标签可能会更好。不幸的是,除了很容易的依赖标签之外,我不经常修改 pom.xml 。因此,我在短时间内通过艰苦的工作变得很好,但从未 touch/make 多年来发生重大变化,忘记了我上次学到的一切。
我在这里找到了解决方案:Maven AspectJ plugin fails to build with Java 9 due to missing tools.jar
实际解决方案: 更改我的 pom.xml 中的几行就成功了:
<groupId>com.github.m50d</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.11.1</version>
<!--
THESE WERE THE ORIGINAL LINES
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.11</version>
-->
我在命令行上想出了这个:
mvn -X clean install
即使是最新版本的 AspectJ 仍在寻找 tools.jar 文件:
<properties>
<!-- Other lines removed for brevity -->
<aspect.version>1.9.1</aspect.version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.machinepublishers/jbrowserdriver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspect.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspect.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspect.version}</version>
</dependency>
<!-- OTHER DEPENDENCIES REMOVED FOR BREVITY -->
</dependencies>
<build>
<plugins>
<!-- Maven Assembly Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<!-- get all project dependencies -->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!-- MainClass in mainfest make a executable jar -->
<archive>
<manifest>
<mainClass>a.b.c.Foo</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
<!-- AspectJ configuration -->
<plugin>
<groupId>com.github.m50d</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.11.1</version>
<!--
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.11</version>
-->
<configuration>
<!-- MUST use 10, NOT 1.10 or ajc breaks -->
<complianceLevel>10</complianceLevel>
<source>10</source>
<target>10</target>
<showWeaveInfo>true</showWeaveInfo>
<verbose>true</verbose>
<Xlint>ignore</Xlint>
<encoding>UTF-8</encoding>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal> <!-- use this goal to weave all your main classes -->
<goal>test-compile</goal> <!-- use this goal to weave all your test classes -->
</goals>
</execution>
</executions>
</plugin>
<!-- -->
</plugins>
</build>
我不得不重新启动大约一年半前运行的旧项目,但现在当我这样做时:
mvn clean install
无论是在命令行上还是通过 Eclipse,它都可以正常编译,但不会在清单中添加 main-class AND 我确实有正确的指令。
我正在使用:
- Apache Maven 3.5.4
- JDK 10.0.2
- eclipse 4.8.0(光子)
所以这是 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.a.b.c</groupId>
<artifactId>JarNameHere</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<log4j.version>2.4</log4j.version>
<http.client.version>4.5.2</http.client.version>
<maven.compiler.source>10</maven.compiler.source>
<maven.compiler.target>10</maven.compiler.target>
</properties>
<dependencies>
<!-- DEPENDENCIES HERE, BUT REMOVED TO MAKE MORE READABLE -->
...
</dependencies>
<build>
<pluginManagement>
<plugins>
<!-- Maven Assembly Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<!-- get all project dependencies -->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!-- MainClass in mainfest make a executable jar -->
<archive>
<manifest>
<mainClass>com.a.b.c.MainClass</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- AspectJ configuration -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.11</version>
<configuration>
<complianceLevel>1.11</complianceLevel>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- -->
</plugins>
</pluginManagement>
</build>
</project>
您犯了一个 class 初学者的错误:您假设 pluginManagement
部分中配置的插件将在构建期间自动使用。事实上,他们不会。 AspectJ Maven 和 Assembly 都不会 运行 像这样,所以当然在你的 target
目录中不会有任何具有依赖项的 JAR。
您也需要将插件显式添加到 plugins
部分,至少通过组 ID 和名称引用它们(那里不需要版本或配置,除非您想覆盖您已经定义的内容pluginManagement
).
除此之外,您会注意到 AspectJ Maven 仍然配置错误,一旦插件被激活,构建就会失败。但这超出了你的问题范围,所以我不打算在这里详细说明。
P.S.: 我将您的设置复制到我自己的 POM 中,修复了它们并且可以确认只要 AspectJ 插件具有正确的设置,Assembly 插件就会按预期工作,包括清单右主 class.
更新:所以你只提供了一个没有任何代码编译的POM片段运行,但你想看我的完整POM。我觉得这有点奇怪,因为实际上你应该提供一个 MCVE。但无论如何,这就是我所拥有的:我只是将你的 Assembly 插件合并到我自己的一个项目中,我通常使用一个 One-JAR 插件(构建一个可执行的 JAR 的 JAR),用你的替换我的插件并检查我是否可以 运行 可执行文件 java -jar ...
。测试成功。不过,我仍然在 JDK 8,对于这个问题,我没有升级我的整个构建系统。对于示例,我还删除了除 AspectJ 运行time.
<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.a.b.c</groupId>
<artifactId>JarNameHere</artifactId>
<version>0.1-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<main-class>com.a.b.c.MainClass</main-class>
<aspectj.version>1.8.13</aspectj.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<pluginManagement>
<plugins>
<!-- Maven Assembly Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<!-- get all project dependencies -->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!-- MainClass in mainfest make a executable jar -->
<archive>
<manifest>
<mainClass>${main-class}</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- AspectJ configuration -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.11</version>
<configuration>
<!--<showWeaveInfo>true</showWeaveInfo>-->
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<Xlint>ignore</Xlint>
<complianceLevel>${maven.compiler.target}</complianceLevel>
<encoding>${project.build.sourceEncoding}</encoding>
<!--<verbose>true</verbose>-->
<!--<warn>constructorName,packageDefaultMethod,deprecation,maskedCatchBlocks,unusedLocals,unusedArguments,unusedImport</warn>-->
</configuration>
<executions>
<execution>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
</dependency>
</dependencies>
</project>
我毫不怀疑@kriegaex 给出的答案会在 Java 9 之前起作用,因为我的老 pom.xml 在 Java 8 上工作了很多年。事实证明我在问题中没有提供足够的信息,无法正确回答这个问题。我从 Java 8 升级到 Java 10 搞砸了 AspectJ 集成,所以它会在 Maven 开始创建 jar 之前失败。
注意:我会继续完善它,因为使用 pluginManagement 标签可能会更好。不幸的是,除了很容易的依赖标签之外,我不经常修改 pom.xml 。因此,我在短时间内通过艰苦的工作变得很好,但从未 touch/make 多年来发生重大变化,忘记了我上次学到的一切。
我在这里找到了解决方案:Maven AspectJ plugin fails to build with Java 9 due to missing tools.jar
实际解决方案: 更改我的 pom.xml 中的几行就成功了:
<groupId>com.github.m50d</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.11.1</version>
<!--
THESE WERE THE ORIGINAL LINES
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.11</version>
-->
我在命令行上想出了这个:
mvn -X clean install
即使是最新版本的 AspectJ 仍在寻找 tools.jar 文件:
<properties>
<!-- Other lines removed for brevity -->
<aspect.version>1.9.1</aspect.version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.machinepublishers/jbrowserdriver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspect.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspect.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspect.version}</version>
</dependency>
<!-- OTHER DEPENDENCIES REMOVED FOR BREVITY -->
</dependencies>
<build>
<plugins>
<!-- Maven Assembly Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<!-- get all project dependencies -->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!-- MainClass in mainfest make a executable jar -->
<archive>
<manifest>
<mainClass>a.b.c.Foo</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
<!-- AspectJ configuration -->
<plugin>
<groupId>com.github.m50d</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.11.1</version>
<!--
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.11</version>
-->
<configuration>
<!-- MUST use 10, NOT 1.10 or ajc breaks -->
<complianceLevel>10</complianceLevel>
<source>10</source>
<target>10</target>
<showWeaveInfo>true</showWeaveInfo>
<verbose>true</verbose>
<Xlint>ignore</Xlint>
<encoding>UTF-8</encoding>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal> <!-- use this goal to weave all your main classes -->
<goal>test-compile</goal> <!-- use this goal to weave all your test classes -->
</goals>
</execution>
</executions>
</plugin>
<!-- -->
</plugins>
</build>