如何使用 Maven 构建 JAR 来指定主 class 和 MANIFEST.MF?

How to specify the main class with MANIFEST.MF using Maven to build a 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>tech.saundersconsulting</groupId>
  <artifactId>mavenchimp</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
    <exec.mainClass>tech.mavenchimp.App</exec.mainClass>
  </properties>
  <repositories>
    <repository>
      <id>java-net-repo</id>
      <url>https://maven.java.net/content/repositories/public/</url>
    </repository>
    <repository>
      <id>jitpack.io</id>
      <url>https://jitpack.io</url>
    </repository>
  </repositories>
  <dependencies>
    <dependency>
      <groupId>com.github.banana-j</groupId>
      <artifactId>bananaj</artifactId>
      <version>0.6.2</version>
    </dependency>
    <dependency>
      <groupId>com.github.NetoDevel</groupId>
      <artifactId>mailchimp-java</artifactId>
      <version>0.1.5</version>
    </dependency>
  </dependencies>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <addClasspath>true</addClasspath>
            <mainClass>tech.mavenchimp.App</mainClass>
          </manifest>
        </archive>
      </configuration>
    </plugin>
  </plugins>
</project>

mvn 错误:

nicholas@mordor:~/NetBeansProjects/mavenchimp$ 
nicholas@mordor:~/NetBeansProjects/mavenchimp$ mvn clean
[INFO] Scanning for projects...
[ERROR] [ERROR] Some problems were encountered while processing the POMs:
[ERROR] Malformed POM /home/nicholas/NetBeansProjects/mavenchimp/pom.xml: Unrecognised tag: 'plugins' (position: START_TAG seen ...</dependencies>\n  <plugins>... @36:12)  @ /home/nicholas/NetBeansProjects/mavenchimp/pom.xml, line 36, column 12
 @ 
[ERROR] The build could not read 1 project -> [Help 1]
[ERROR]   
[ERROR]   The project tech.saundersconsulting:mavenchimp:1.0-SNAPSHOT (/home/nicholas/NetBeansProjects/mavenchimp/pom.xml) has 1 error
[ERROR]     Malformed POM /home/nicholas/NetBeansProjects/mavenchimp/pom.xml: Unrecognised tag: 'plugins' (position: START_TAG seen ...</dependencies>\n  <plugins>... @36:12)  @ /home/nicholas/NetBeansProjects/mavenchimp/pom.xml, line 36, column 12 -> [Help 2]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/ProjectBuildingException
[ERROR] [Help 2] http://cwiki.apache.org/confluence/display/MAVEN/ModelParseException
nicholas@mordor:~/NetBeansProjects/mavenchimp$ 

如果我去掉插件节点,项目至少构建:

nicholas@mordor:~/NetBeansProjects/mavenchimp$ 
nicholas@mordor:~/NetBeansProjects/mavenchimp$ mvn clean package
[INFO] Scanning for projects...
[INFO] 
[INFO] -----------------< tech.saundersconsulting:mavenchimp >-----------------
[INFO] Building mavenchimp 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ mavenchimp ---
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mavenchimp ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/nicholas/NetBeansProjects/mavenchimp/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ mavenchimp ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to /home/nicholas/NetBeansProjects/mavenchimp/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ mavenchimp ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/nicholas/NetBeansProjects/mavenchimp/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ mavenchimp ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mavenchimp ---
[INFO] No tests to run.
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ mavenchimp ---
[INFO] Building jar: /home/nicholas/NetBeansProjects/mavenchimp/target/mavenchimp-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.658 s
[INFO] Finished at: 2020-12-07T02:24:18-08:00
[INFO] ------------------------------------------------------------------------
nicholas@mordor:~/NetBeansProjects/mavenchimp$ 

但是,我希望在上面的 JAR 的清单中指定主要的 class。

<plugins> 块需要驻留在 <build> 块内。

我不确定你的问题是关于 jar 的,但是你的构建失败了,因为你直接放置了标签而不是在构建标签中。应该是 -

<build>
   <plugins>
       <plugin>
       </plugin>
   </plugins> 
</build>

在“uber jar”位中添加:

nicholas@mordor:~/NetBeansProjects/mavenchimp$ 
nicholas@mordor:~/NetBeansProjects/mavenchimp$ cat 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>tech.saundersconsulting</groupId>
  <artifactId>mavenchimp</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
    <exec.mainClass>tech.mavenchimp.App</exec.mainClass>
  </properties>
  <repositories>
    <repository>
      <id>java-net-repo</id>
      <url>https://maven.java.net/content/repositories/public/</url>
    </repository>
    <repository>
      <id>jitpack.io</id>
      <url>https://jitpack.io</url>
    </repository>
  </repositories>
  <dependencies>
    <dependency>
      <groupId>com.github.banana-j</groupId>
      <artifactId>bananaj</artifactId>
      <version>0.6.2</version>
    </dependency>
    <dependency>
      <groupId>com.github.NetoDevel</groupId>
      <artifactId>mailchimp-java</artifactId>
      <version>0.1.5</version>
    </dependency>
  </dependencies>
  <build>
    <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>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <mainClass>tech.mavenchimp.App</mainClass>
            </manifest>
          </archive>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>
nicholas@mordor:~/NetBeansProjects/mavenchimp$ 
nicholas@mordor:~/NetBeansProjects/mavenchimp$ mvn clean package;java -jar target/mavenchimp-1.0-SNAPSHOT.jar 
[INFO] Scanning for projects...
[INFO] 
[INFO] -----------------< tech.saundersconsulting:mavenchimp >-----------------
[INFO] Building mavenchimp 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ mavenchimp ---
[INFO] Deleting /home/nicholas/NetBeansProjects/mavenchimp/target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mavenchimp ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/nicholas/NetBeansProjects/mavenchimp/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ mavenchimp ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to /home/nicholas/NetBeansProjects/mavenchimp/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ mavenchimp ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/nicholas/NetBeansProjects/mavenchimp/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ mavenchimp ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mavenchimp ---
[INFO] No tests to run.
[INFO] 
[INFO] --- maven-jar-plugin:2.3.2:jar (default-jar) @ mavenchimp ---
[INFO] Building jar: /home/nicholas/NetBeansProjects/mavenchimp/target/mavenchimp-1.0-SNAPSHOT.jar
[INFO] 
[INFO] --- maven-shade-plugin:3.2.4:shade (default) @ mavenchimp ---
[INFO] Including com.github.banana-j:bananaj:jar:0.6.2 in the shaded jar.
[INFO] Including commons-codec:commons-codec:jar:1.11 in the shaded jar.
[INFO] Including net.sourceforge.jexcelapi:jxl:jar:2.6.12 in the shaded jar.
[INFO] Including log4j:log4j:jar:1.2.14 in the shaded jar.
[INFO] Including org.json:json:jar:20180130 in the shaded jar.
[INFO] Including org.apache.httpcomponents:httpclient:jar:4.5.5 in the shaded jar.
[INFO] Including org.apache.httpcomponents:httpcore:jar:4.4.9 in the shaded jar.
[INFO] Including commons-logging:commons-logging:jar:1.2 in the shaded jar.
[INFO] Including com.github.NetoDevel:mailchimp-java:jar:0.1.5 in the shaded jar.
[INFO] Including com.google.code.gson:gson:jar:2.3.1 in the shaded jar.
[INFO] Including com.netflix.feign:feign-gson:jar:8.17.0 in the shaded jar.
[INFO] Including com.netflix.feign:feign-core:jar:8.17.0 in the shaded jar.
[INFO] Including org.jvnet:animal-sniffer-annotation:jar:1.0 in the shaded jar.
[INFO] Including junit:junit:jar:4.12 in the shaded jar.
[INFO] Including org.hamcrest:hamcrest-core:jar:1.3 in the shaded jar.
[WARNING] animal-sniffer-annotation-1.0.jar, bananaj-0.6.2.jar, commons-codec-1.11.jar, commons-logging-1.2.jar, feign-core-8.17.0.jar, feign-gson-8.17.0.jar, gson-2.3.1.jar, hamcrest-core-1.3.jar, httpclient-4.5.5.jar, httpcore-4.4.9.jar, json-20180130.jar, junit-4.12.jar, jxl-2.6.12.jar, log4j-1.2.14.jar, mailchimp-java-0.1.5.jar, mavenchimp-1.0-SNAPSHOT.jar define 1 overlapping resource: 
[WARNING]   - META-INF/MANIFEST.MF
[WARNING] commons-codec-1.11.jar, commons-logging-1.2.jar define 2 overlapping resources: 
[WARNING]   - META-INF/LICENSE.txt
[WARNING]   - META-INF/NOTICE.txt
[WARNING] httpclient-4.5.5.jar, httpcore-4.4.9.jar, log4j-1.2.14.jar define 2 overlapping resources: 
[WARNING]   - META-INF/LICENSE
[WARNING]   - META-INF/NOTICE
[WARNING] httpclient-4.5.5.jar, httpcore-4.4.9.jar define 1 overlapping resource: 
[WARNING]   - META-INF/DEPENDENCIES
[WARNING] maven-shade-plugin has detected that some class files are
[WARNING] present in two or more JARs. When this happens, only one
[WARNING] single version of the class is copied to the uber jar.
[WARNING] Usually this is not harmful and you can skip these warnings,
[WARNING] otherwise try to manually exclude artifacts based on
[WARNING] mvn dependency:tree -Ddetail=true and the above output.
[WARNING] See http://maven.apache.org/plugins/maven-shade-plugin/
[INFO] Replacing original artifact with shaded artifact.
[INFO] Replacing /home/nicholas/NetBeansProjects/mavenchimp/target/mavenchimp-1.0-SNAPSHOT.jar with /home/nicholas/NetBeansProjects/mavenchimp/target/mavenchimp-1.0-SNAPSHOT-shaded.jar
[INFO] Dependency-reduced POM written at: /home/nicholas/NetBeansProjects/mavenchimp/dependency-reduced-pom.xml
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  4.176 s
[INFO] Finished at: 2020-12-07T05:24:05-08:00
[INFO] ------------------------------------------------------------------------
Dec 07, 2020 5:24:05 AM tech.mavenchimp.App runApp
INFO: running..
log4j:WARN No appenders could be found for logger (com.github.alexanderwe.bananaj.connection.Connection).
log4j:WARN Please initialize the log4j system properly.
Dec 07, 2020 5:24:06 AM tech.mavenchimp.Chimp connect
INFO: test
nicholas@mordor:~/NetBeansProjects/mavenchimp$ 

以便 jar 独立运行。