【问题标题】:maven jar not including main in manifestmaven jar 不包括清单中的 main
【发布时间】:2019-01-20 18:34:42
【问题描述】:

我不得不重振一个大约一年半前工作的旧项目,但现在我这样做了:

mvn clean install

无论是在命令行上还是通过 Eclipse,它都可以正常编译,但不会在清单中添加主类 并且我确实有正确的指令。

我正在使用:

  • 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>

【问题讨论】:

  • 我缺少maven-assembly-plugin的版本
  • @RobertScholte,该版本是在 Maven Super POM 中定义的,这不是问题。

标签: java maven aspectj aspectj-maven-plugin


【解决方案1】:

您犯了一个经典的初学者错误:您假设在pluginManagement 部分中配置的插件将在构建期间自动使用。事实上,他们不会。 AspectJ Maven 和 Assembly 都不会像这样运行,所以在您的 target 目录中当然不会有任何具有依赖关系的 JAR。

您还需要将插件显式添加到 plugins 部分,至少通过组 ID 和名称引用它们(那里不需要版本或配置,除非您想覆盖您已经在 pluginManagement 中定义的内容) .

此外,您会注意到 AspectJ Maven 仍然配置错误,并且一旦激活插件,构建就会失败。但这超出了你的问题范围,所以我不打算在这里详细说明。

P.S.:我将您的设置复制到我自己的 POM 中,并对其进行了修复,并且可以确认只要 AspectJ 插件具有正确的设置,Assembly 插件就会按预期完成其工作,包括具有正确主类的清单。


更新:所以你只提供了一个 POM 片段,没有任何代码来编译和运行,但你想看到我的完整 POM。我觉得这有点奇怪,因为实际上你应该提供一个MCVE。但无论如何,这就是我所拥有的:我刚刚将您的程序集插件合并到我自己的一个项目中,我通常使用 One-JAR 插件(构建 JAR 的可执行 JAR),用您的插件替换我的插件并检查我是否可以运行可使用java -jar ... 执行。测试成功。不过,我仍在使用 JDK 8,对于这个问题,我没有升级我的整个构建系统。对于该示例,我还剥离了除 AspectJ 运行时之外的所有其他依赖项。

<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>

【讨论】:

  • 您能否更新您的答案以显示您如何使其工作(并包括 AspectJ)。这个设置工作了 9 年,直到更新到 Maven 3(或 JDK 10),所以它要么是 Maven 更新/更改了一些规则,要么它在之前的编译中比较松散并且他们收紧了它。现在使用 Maven 3,工作了近十年的 pom.xml 停止工作
  • 完成。但是,嘿,最好不要仅仅声称你之前拥有的东西正在工作(因为除非你在剪切和粘贴之后修改你的 POM,否则这是不可能的),而是真正向我们展示你拥有的东西。
  • 我玩了几个小时,结果发现真正的原因是从 Java 8 升级到 JDK 10。在我的旧笔记本电脑上,我有一个旧版本的 Maven 3,所以它不是专家。这对我有用:com.github.m50daspectj-maven-plugin1.11.1
  • 我在下面发布了更多内容
  • 但是您自己的回答表明我对您缺少的插件部分是正确的(或者您只是没有将其复制到您的问题中),我仍然确定您在问题中显示的 AspectJ 配置不是100% 正确,Java9 之前与否。当前的 AspectJ Maven 版本 1.11 不适用于 Java 10 或 11 是一个众所周知的事实,事实上我本可以告诉您当前您需要使用 fork。我使用的也兼容 Java 11 的是com.nickwongdev:aspectj-maven-plugin:1.12.1,它正在等待作为 PR 包含在上游的 AspectJ Maven 中。
【解决方案2】:

我毫不怀疑@kriegaex 给出的答案将在 Java 9 之前有效,因为我的旧 pom.xml 在 Java 8 上工作了多年。事实证明 我没有提供足够的信息 在我的问题中正确回答这个问题。是我从 Java 8 升级到 Java 10 搞砸了 AspectJ 集成,所以在 Maven 创建 jar 之前它会失败。

注意:我将继续完善这一点,因为使用 pluginManagement 标记可能会更好。不幸的是,我不会修改 pom.xml,除了依赖标签之外,这很容易。因此,我在艰难的事情上工作了一小段时间,但多年来从未接触/做出重大改变,忘记了我之前学到的所有东西。

我在这里找到了解决方案: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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-31
    • 1970-01-01
    • 2018-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-27
    • 1970-01-01
    相关资源
    最近更新 更多