从依赖于 Shade 或 Maven Assembly Plugin 的 Maven 项目构建可执行 Jar

Build an executable Jar from a Maven Project with dependencies with Shade or Maven Assembly Plugin

我正在尝试创建一个打包了依赖项的可执行 jar。到目前为止,我一直收到“无主要清单属性”。有谁知道我做错了什么?

我在 POM 的顶部添加了包装 jar

  <groupId>com.moga</groupId>
  <artifactId>capacityplanfacilities</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

根据文档,这是我测试过的 2 种方法(Maven Shade 和 Apache Maven Assembly):

  1. Maven Shade
    <build>
    <!--    <sourceDirectory>${basedir}/src/main/java</sourceDirectory>-->
    <!--    <testSourceDirectory>${basedir}/src/test/java</testSourceDirectory>-->
        <outputDirectory>${basedir}/target/classes</outputDirectory>
    <!--    Do not work for when running tests-->
    <!--    <testOutputDirectory>${basedir}/target/test-classes</testOutputDirectory>-->
    
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
          <plugins>
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-shade-plugin</artifactId>
              <version>3.2.4</version>
              <executions>
                <execution>
                  <phase>package</phase>
                  <goals>
                    <goal>shade</goal>
                  </goals>
                  <configuration>
                    <transformers>
                      <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>com.moga.planning.problem.CapacityPlanningApp</mainClass>
                      </transformer>
                    </transformers>
                  </configuration>
                </execution>
              </executions>
            </plugin>
            <plugin>
              <artifactId>maven-clean-plugin</artifactId>
              <version>3.1.0</version>
            </plugin>
            <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
            <plugin>
              <artifactId>maven-resources-plugin</artifactId>
              <version>3.0.2</version>
            </plugin>
            <plugin>
              <artifactId>maven-compiler-plugin</artifactId>
              <version>3.8.0</version>
            </plugin>
            <plugin>
              <artifactId>maven-surefire-plugin</artifactId>
              <version>2.22.1</version>
            </plugin>
    
            <!--        As per Junit5 Doc https://junit.org/junit5/docs/current/user-guide/#running-tests-build-maven-engines-configure-->
            <plugin>
              <artifactId>maven-failsafe-plugin</artifactId>
              <version>2.22.2</version>
            </plugin>
            <!--        End Junit5-->
    
            <plugin>
              <artifactId>maven-jar-plugin</artifactId>
              <version>3.0.2</version>
            </plugin>
            <plugin>
              <artifactId>maven-install-plugin</artifactId>
              <version>2.5.2</version>
            </plugin>
            <plugin>
              <artifactId>maven-deploy-plugin</artifactId>
              <version>2.8.2</version>
            </plugin>
            <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
            <plugin>
              <artifactId>maven-site-plugin</artifactId>
              <version>3.7.1</version>
            </plugin>
            <plugin>
              <artifactId>maven-project-info-reports-plugin</artifactId>
              <version>3.0.0</version>
            </plugin>
          </plugins>
        </pluginManagement>
      </build>
    </project>
  1. Test 2.2 Apache Maven 程序集插件
    <build>
        <!--    <sourceDirectory>${basedir}/src/main/java</sourceDirectory>-->
        <!--    <testSourceDirectory>${basedir}/src/test/java</testSourceDirectory>-->
            <outputDirectory>${basedir}/target/classes</outputDirectory>
        <!--    Do not work for when running tests-->
        <!--    <testOutputDirectory>${basedir}/target/test-classes</testOutputDirectory>-->
        
            <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
              <plugins>
                <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-assembly-plugin</artifactId>
                  <executions>
                    <execution>
                      <phase>package</phase>
                      <goals>
                        <goal>single</goal>
                      </goals>
                      <configuration>
                        <archive>
                          <manifest>
                            <mainClass>com.moga.planning.problem.CapacityPlanningApp</mainClass>
                          </manifest>
                        </archive>
                        <descriptorRefs>
                          <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                      </configuration>
                    </execution>
                  </executions>
                </plugin>
                <plugin>
                  <artifactId>maven-clean-plugin</artifactId>
                  <version>3.1.0</version>
                </plugin>
                <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
                <plugin>
                  <artifactId>maven-resources-plugin</artifactId>
                  <version>3.0.2</version>
                </plugin>
                <plugin>
                  <artifactId>maven-compiler-plugin</artifactId>
                  <version>3.8.0</version>
                </plugin>
                <plugin>
                  <artifactId>maven-surefire-plugin</artifactId>
                  <version>2.22.1</version>
                </plugin>
        
                <!--        As per Junit5 Doc https://junit.org/junit5/docs/current/user-guide/#running-tests-build-maven-engines-configure-->
                <plugin>
                  <artifactId>maven-failsafe-plugin</artifactId>
                  <version>2.22.2</version>
                </plugin>
                <!--        End Junit5-->
        
                <plugin>
                  <artifactId>maven-jar-plugin</artifactId>
                  <version>3.0.2</version>
                </plugin>
                <plugin>
                  <artifactId>maven-install-plugin</artifactId>
                  <version>2.5.2</version>
                </plugin>
                <plugin>
                  <artifactId>maven-deploy-plugin</artifactId>
                  <version>2.8.2</version>
                </plugin>
                <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
                <plugin>
                  <artifactId>maven-site-plugin</artifactId>
                  <version>3.7.1</version>
                </plugin>
                <plugin>
                  <artifactId>maven-project-info-reports-plugin</artifactId>
                  <version>3.0.0</version>
                </plugin>
              </plugins>
            </pluginManagement>
          </build>

非常感谢

问题正在通过修改清单的路径解决。我在 Intellij 中测试了 Manifest 文件的内置生成,发现问题可能与 Manifest 的位置有关,我添加到与 pom 相同的级别,而 intellij 设置在 java 文件夹中.