如何使用maven生成能夠執行的jar?

不依賴任何外界包,maven如何生成能夠執行的jar?

pom中不包含任何引用的狀況下,只須要在pom中添加 maven-jar-plugin便可。java

解決過程

新建項目,僅包含一個main函數類:Xixi.java,輸出 Xixi Say: hello pom中配置爲apache

<groupId>com.paxi</groupId>
  <artifactId>xixi</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>xixi</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
複製代碼

執行 mvn clean install 生成jar包 xixi-1.0-SNAPSHOT.jar,執行jar命令,執行反饋出錯bash

⇒  java -jar xixi-1.0-SNAPSHOT.jar
xixi-1.0-SNAPSHOT.jar中沒有主清單屬性
複製代碼

對jar進行解壓縮jar xvf xixi-1.0-SNAPSHOT.jar查看對應文件MANIFEST.MFmaven

⇒  cat META-INF/MANIFEST.MF
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: paxi
Created-By: Apache Maven 3.3.3
Build-Jdk: 1.8.0_121
複製代碼

在pom中添加插件 maven-jar-plugin函數

<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.1.0</version>
        <configuration>
          <archive>
            <manifest>
              <mainClass>com.paxi.Xixi</mainClass>
            </manifest>
          </archive>
        </configuration>
      </plugin>
    </plugins>
  </build>
複製代碼

執行成功輸出測試

⇒  java -jar xixi-1.0-SNAPSHOT.jar
Xixi Say: hello
複製代碼

再對解壓縮後的MANIFEST.MF文件查看爲ui

⇒  cat META-INF/MANIFEST.MF
Manifest-Version: 1.0
Built-By: paxi
Created-By: Apache Maven 3.3.3
Build-Jdk: 1.8.0_121
Main-Class: com.paxi.Xixi  //新增
複製代碼

在pom中添加依賴,maven如何生成可執行jar?

須要使用插件maven-dependency-pluginmaven-jar-pluginurl

解決過程

在pom中僅僅增長一個apache-commons包spa

<groupId>com.paxi</groupId>
  <artifactId>xixi</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>xixi</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>

      <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.7</version>
    </dependency>

  </dependencies>
複製代碼

在代碼中運用common包中的一個類.net

public static void main(String[] args) {
        String words = "Xixi Say: hello";
        if (StringUtils.isNotBlank(words))
        {
            System.out.println(words);
        }
    }
複製代碼

打包後直接運行出錯

⇒  java -jar xixi-1.0-SNAPSHOT.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils
	at com.paxi.Xixi.main(Xixi.java:11)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang3.StringUtils
	at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
	... 1 more
複製代碼

在pom中增長插件

<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.1.0</version>
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <mainClass>com.paxi.Xixi</mainClass>
              <classpathPrefix>libs/</classpathPrefix>
            </manifest>
          </archive>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>3.1.1</version>
        <executions>
          <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
              <goal>copy-dependencies</goal>
            </goals>
            <configuration>
              <outputDirectory>
                ${project.build.directory}/libs
              </outputDirectory>
            </configuration>
          </execution>
        </executions>
      </plugin>

    </plugins>
  </build>
複製代碼

再次運行

⇒  java -jar xixi-1.0-SNAPSHOT.jar
Xixi Say: hello
複製代碼

MANIFEST.MF文件爲

⇒  cat META-INF/MANIFEST.MF
Manifest-Version: 1.0
Built-By: paxi
Class-Path: libs/commons-lang3-3.7.jar
Created-By: Apache Maven 3.3.3
Build-Jdk: 1.8.0_121
Main-Class: com.paxi.Xixi
複製代碼

不只使用了POM中的依賴還使用了三方的jar,maven如何生成可執行jar?

編譯過程須要對編譯插件 maven-compiler-plugin作修改,運行的時候須要加上三方jar的目錄,執行命令

⇒  java -jar -Djava.ext.dirs=lib/ target/xixi-1.0-SNAPSHOT.jar
Xixi Say: hello
Papa say:hello
複製代碼

編譯解決過程

測試代碼爲

public static void main(String[] args) {
        String words = "Xixi Say: hello";
        if (StringUtils.isNotBlank(words))
        {
            System.out.println(words);
        }
        Papa.say();
    }
複製代碼

使用mvn命令執行報錯

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project xixi: Compilation failure
xixi/src/main/java/com/paxi/Xixi.java:[15,9] 找不到符號
複製代碼

pom中添加編譯插件

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <compilerArgs>
            <arg>-Djava.ext.dirs=lib/</arg>
          </compilerArgs>
        </configuration>
      </plugin>
複製代碼

打包執行

⇒  java -jar target/xixi-1.0-SNAPSHOT.jar
Xixi Say: hello
Exception in thread "main" java.lang.NoClassDefFoundError: com/paxi/Papa
	at com.paxi.Xixi.main(Xixi.java:15)
Caused by: java.lang.ClassNotFoundException: com.paxi.Papa
	at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
	... 1 more
複製代碼

加上參數後便可得出結果

⇒  java -jar -Djava.ext.dirs=lib/ target/xixi-1.0-SNAPSHOT.jar
Xixi Say: hello
Papa say:hello
複製代碼
相關文章
相關標籤/搜索