0、工程样例
新建一个如下的Module。
Compute.java
:
package com.lfqy.fatjar.util;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
/**
* Created by chengxia on 2020/10/17.
*/
public class Compute {
public static Logger log4jLogger = Logger.getLogger(Compute.class);
public Compute() {
//PropertyConfigurator.configure ( "log4j.properties");
}
public static int add(int a, int b){
log4jLogger.info("Accepted: " + a + " + " + b);
return a + b;
}
public static int minus(int a, int b){
log4jLogger.info("Accepted: " + a + " - " + b);
return a - b;
}
}
App.java
:
package com.lfqy.fatjar.util;
/**
* Created by chengxia on 2020/10/18.
*/
public class App {
public static void main(String []args){
int a = 3;
int b = 6;
System.out.println(Compute.add(a,b));
System.out.println(Compute.minus(a,b));
}
}
log4j.properties
:
log4j.rootLogger=ALL, Log2Console, Log2File
log4j.appender.Log2Console=org.apache.log4j.ConsoleAppender
log4j.appender.Log2Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Log2Console.layout.ConversionPattern=%-4r %-5p [%t] %37c %3x - %m%n
log4j.appender.Log2File = org.apache.log4j.FileAppender
log4j.appender.Log2File.File = log4j.log
log4j.appender.Log2File.Encoding=UTF-8
log4j.appender.Log2File.layout=org.apache.log4j.PatternLayout
log4j.appender.Log2File.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss}[ %p ]%m%n
log4j.appender.Log2File.append = true
这个文件需要注意,在Maven项目中,log4j.properties
配置文件放在resources目录下可以缺省被识别。
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.lfqy.fatjar</groupId>
<artifactId>packfatjar</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.11</version>
</dependency>
</dependencies>
</project>
1、简单打包
在Module的根目录执行打包命令mvn package
:
$ pwd
/Users/chengxia/Developer/Java/JavaAppProject/packfatjar
$ mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------------< com.lfqy.fatjar:packfatjar >---------------------
[INFO] Building packfatjar 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ packfatjar ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ packfatjar ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 2 source files to /Users/chengxia/Developer/Java/JavaAppProject/packfatjar/target/classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ packfatjar ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/chengxia/Developer/Java/JavaAppProject/packfatjar/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ packfatjar ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ packfatjar ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ packfatjar ---
[INFO] Building jar: /Users/chengxia/Developer/Java/JavaAppProject/packfatjar/target/packfatjar-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.376 s
[INFO] Finished at: 2020-10-18T05:35:50+08:00
[INFO] ------------------------------------------------------------------------
$
执行完成之后,可以看到jar包已经生成:
这样打包完成之后,可以通过指定主类和classpath的方式运行:
$ java -classpath /Users/chengxia/.m2/repository/log4j/log4j/1.2.11/log4j-1.2.11.jar:target/packfatjar-1.0-SNAPSHOT.jar com.lfqy.fatjar.util.App
0 INFO [main] com.lfqy.fatjar.util.Compute - Accepted: 3 + 6
9
1 INFO [main] com.lfqy.fatjar.util.Compute - Accepted: 3 - 6
-3
$
从上面可以看出,这种jar包在运行时,需要指定主类并且指定依赖的jar包,比较麻烦。下面介绍Maven打包jar包时,如何制定主类。
2、通过Maven在打jar包时指定主类
我们通过修改Module的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.lfqy.fatjar</groupId>
<artifactId>packfatjar</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.11</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<classesDirectory>target/classes/</classesDirectory>
<archive>
<manifest>
<!-- 主函数的入口 -->
<mainClass>com.lfqy.fatjar.util.App</mainClass>
<!-- 打包时 MANIFEST.MF文件不记录的时间戳版本 -->
<!--<useUniqueVersions>false</useUniqueVersions>-->
<!--<addClasspath>true</addClasspath>-->
<!--<classpathPrefix>lib/</classpathPrefix>-->
</manifest>
<manifestEntries>
<Class-Path>./log4j-1.2.11.jar</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
执行打包操作:
$ mvn package
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for com.lfqy.fatjar:packfatjar:jar:1.0-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-jar-plugin is missing. @ line 22, column 21
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]
[INFO] ---------------------< com.lfqy.fatjar:packfatjar >---------------------
[INFO] Building packfatjar 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ packfatjar ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ packfatjar ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 2 source files to /Users/chengxia/Developer/Java/JavaAppProject/packfatjar/target/classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ packfatjar ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/chengxia/Developer/Java/JavaAppProject/packfatjar/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ packfatjar ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ packfatjar ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ packfatjar ---
[INFO] Building jar: /Users/chengxia/Developer/Java/JavaAppProject/packfatjar/target/packfatjar-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.720 s
[INFO] Finished at: 2020-10-18T06:54:38+08:00
[INFO] ------------------------------------------------------------------------
$
打包完成之后,可以在jar包中看到文件META-INF/MANIFEST.MF
,内容如下:
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: chengxia
Class-Path: ./log4j-1.2.11.jar
Created-By: Apache Maven 3.6.0
Build-Jdk: 1.8.0_181
Main-Class: com.lfqy.fatjar.util.App
对于单独运行的jar包,需要在jar/META-INF/MANIFEST.MF文件里设置classpath,这样程序才能从classpath中加载文件。对于运行jar包,在环境变量里设置的classpath是无效的。这就是为什么需要在jar包中设置classpath。
注意:
这里的“.”路径指的是运行的jar包所在的目录。所以,在实际运行这个jar包时,需要将依赖的jar包同步拷贝到运行jar包所在的目录。如下示例。
$ java -jar target/pac
packfatjar-1.0-SNAPSHOT/ packfatjar-1.0-SNAPSHOT.jar
localhost:packfatjar chengxia$ java -jar target/packfatjar-1.0-SNAPSHOT.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger
at com.lfqy.fatjar.util.Compute.<clinit>(Compute.java:10)
at com.lfqy.fatjar.util.App.main(App.java:10)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 2 more
$ cp /Users/chengxia/.m2/repository/log4j/log4j/1.2.11/log4j-1.2.11.jar target/
localhost:packfatjar chengxia$ java -jar target/packfatjar-1.0-SNAPSHOT.jar
0 INFO [main] com.lfqy.fatjar.util.Compute - Accepted: 3 + 6
9
2 INFO [main] com.lfqy.fatjar.util.Compute - Accepted: 3 - 6
-3
$
3、通过Maven生成fatjar
将jar包依赖的所有内容都打入一个jar包内,这样生成的jar包叫做fatjar。下面演示如何通过Maven打包fatjar。
首先,修改Module的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.lfqy.fatjar</groupId>
<artifactId>packfatjar</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.11</version>
</dependency>
</dependencies>
<build>
<plugins>
<!--<plugin>-->
<!--<groupId>org.apache.maven.plugins</groupId>-->
<!--<artifactId>maven-jar-plugin</artifactId>-->
<!--<configuration>-->
<!--<classesDirectory>target/classes/</classesDirectory>-->
<!--<archive>-->
<!--<manifest>-->
<!--<!– 主函数的入口 –>-->
<!--<mainClass>com.lfqy.fatjar.util.App</mainClass>-->
<!--<!– 打包时 MANIFEST.MF文件不记录的时间戳版本 –>-->
<!--<!–<useUniqueVersions>false</useUniqueVersions>–>-->
<!--<!–<addClasspath>true</addClasspath>–>-->
<!--<!–<classpathPrefix>lib/</classpathPrefix>–>-->
<!--</manifest>-->
<!--<manifestEntries>-->
<!--<Class-Path>./log4j-1.2.11.jar</Class-Path>-->
<!--</manifestEntries>-->
<!--</archive>-->
<!--</configuration>-->
<!--</plugin>-->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<!-- 此处指定main方法入口的class -->
<mainClass>com.lfqy.fatjar.util.App</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>assembly</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
执行打包命令:
$ mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------------< com.lfqy.fatjar:packfatjar >---------------------
[INFO] Building packfatjar 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ packfatjar ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ packfatjar ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 2 source files to /Users/chengxia/Developer/Java/JavaAppProject/packfatjar/target/classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ packfatjar ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/chengxia/Developer/Java/JavaAppProject/packfatjar/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ packfatjar ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ packfatjar ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ packfatjar ---
[INFO] Building jar: /Users/chengxia/Developer/Java/JavaAppProject/packfatjar/target/packfatjar-1.0-SNAPSHOT.jar
[INFO]
[INFO] >>> maven-assembly-plugin:2.2-beta-5:assembly (make-assembly) > package @ packfatjar >>>
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ packfatjar ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ packfatjar ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ packfatjar ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/chengxia/Developer/Java/JavaAppProject/packfatjar/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ packfatjar ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ packfatjar ---
[INFO] No tests to run.
[INFO] Skipping execution of surefire because it has already been run for this configuration
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ packfatjar ---
[INFO]
[INFO] <<< maven-assembly-plugin:2.2-beta-5:assembly (make-assembly) < package @ packfatjar <<<
[INFO]
[INFO]
[INFO] --- maven-assembly-plugin:2.2-beta-5:assembly (make-assembly) @ packfatjar ---
[INFO] Building jar: /Users/chengxia/Developer/Java/JavaAppProject/packfatjar/target/packfatjar-1.0-SNAPSHOT.jar
[WARNING] Configuration options: 'appendAssemblyId' is set to false, and 'classifier' is missing.
Instead of attaching the assembly file: /Users/chengxia/Developer/Java/JavaAppProject/packfatjar/target/packfatjar-1.0-SNAPSHOT.jar, it will become the file for main project artifact.
NOTE: If multiple descriptors or descriptor-formats are provided for this project, the value of this file will be non-deterministic!
[WARNING] Replacing pre-existing project main-artifact file: /Users/chengxia/Developer/Java/JavaAppProject/packfatjar/target/packfatjar-1.0-SNAPSHOT.jar
with assembly file: /Users/chengxia/Developer/Java/JavaAppProject/packfatjar/target/packfatjar-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.571 s
[INFO] Finished at: 2020-10-18T09:53:08+08:00
[INFO] ------------------------------------------------------------------------
localhost:packfatjar chengxia$ tree target/
.DS_Store classes/ maven-status/ packfatjar-1.0-SNAPSHOT.jar
archive-tmp/ maven-archiver/ packfatjar-1.0-SNAPSHOT/
localhost:packfatjar chengxia$ tree target/pac
packfatjar-1.0-SNAPSHOT/ packfatjar-1.0-SNAPSHOT.jar
$
打包生成的jar包结构如下(-L 3
的意思是向下指定三层目录):
$ tree -L 3 target/packfatjar-1.0-SNAPSHOT
target/packfatjar-1.0-SNAPSHOT
├── META-INF
│ └── MANIFEST.MF
├── com
│ └── lfqy
│ └── fatjar
├── log4j.properties
└── org
└── apache
└── log4j
7 directories, 2 files
$
从上面的目录结构可以看出,对于运行所需要的log4j的内容,也被打到了同一个jar包中。jar包中看到文件META-INF/MANIFEST.MF
,内容如下:
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: chengxia
Build-Jdk: 1.8.0_181
Main-Class: com.lfqy.fatjar.util.App
接下来运行的时候,命令就简洁很多了。如下:
$ java -jar target/packfatjar-1.0-SNAPSHOT.jar
0 INFO [main] com.lfqy.fatjar.util.Compute - Accepted: 3 + 6
9
1 INFO [main] com.lfqy.fatjar.util.Compute - Accepted: 3 - 6
-3
$