繁体   English   中英

Maven jar 不包括清单中的 main

[英]maven jar not including main in manifest

我不得不恢复一个大约一年半前有效的旧项目,但现在当我这样做时:

mvn clean install

在命令行或通过Eclipse上,它编译罚款,但在清单中不添加主类我也有适当的指令。

我正在使用:

  • Apache Maven 3.5.4
  • JDK 10.0.2
  • 日食 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>

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

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

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

PS:我将您的设置复制到我自己的 POM 中,修复了它们,并且可以确认一旦 AspectJ 插件具有正确的设置,Assembly 插件就会按预期执行其工作,包括具有正确主类的清单。


更新:所以你只提供一个 POM 片段,没有任何代码来编译和运行,但你想看到我的完整 POM。 我觉得这有点奇怪,因为实际上您应该提供MCVE 但无论如何,这就是我所拥有的:我只是将您的 Assembly Plugin 合并到我自己的一个项目中,在该项目中我通常使用 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>

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

注意:我将继续完善它,因为使用 pluginManagement 标签可能会更好。 不幸的是,除了很容易的依赖标记之外,我通常不会修改 pom.xml。 所以我在短期内通过艰难的事情变得很好,但多年来从未接触/做出重大改变,忘记了我以前学到的一切。

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

暂无
暂无

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

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