因爲今天一個朋友學習過程中用到了maven項目編寫代碼,到了最後打包階段打的包不能運行,一時我也沒想起來具體操作步驟,後來我百度學習了一下,特此記錄下,以便後續自己查閱。
參考資料:
https://blog.csdn.net/fengyun_yi_pure/article/details/70313603
https://blog.csdn.net/xyr05288/article/details/52797493
https://blog.csdn.net/rambo_china/article/details/7691409
https://blog.csdn.net/semial/article/details/75226448
maven項目中不可避免的需要用到依賴jar,實際使用中有的能從maven倉庫找到,有的找不到,所以存在使用本地jar的情況,下面將對使用maven倉庫中jar,以及使用本地jar不同情況下打包可運行jar進行介紹。
情景一:使用maven依賴,所有的依賴都從maven倉庫查找下載,最終打包成可執行jar,需要修改pom文件如下。
<!--使用maven依賴查找 json-lib -->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
對應修改pom文件,需指定程序入口,不然會報錯 xxxx-1.0-SNAPSHOT.jar中沒有主清單屬性
<!--情景一,制定程序入口即可。-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.study.App</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
打包之後,即可通過 java -jar xxx.jar 運行,
package com.study;
import net.sf.json.JSONObject;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
JSONObject json = new JSONObject();
json.put("name","子落");
System.out.println(json.toString());
}
}
運行效果如下,
通過jar可以看出,是將引用的jar中對應的文件解壓編譯到打包的jar中
最終pom文件如下,
<?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.study</groupId>
<artifactId>mvnstudy</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>mvnstudy</name>
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--使用maven依賴查找 json-lib -->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
</dependencies>
<build>
<plugins>
<!--情景一,制定程序入口即可。-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.study.App</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
情景二,本地jar文件及maven依賴混用時打包。
首先介紹下,接下來使用的 testJar-1.0.jar,這個爲我隨便編寫的一個jar文件,模擬本地jar使用場景。
package com.company;
public class Mp {
public static void println(String str){
System.out.println("Mp println = [" + str + "]");
}
}
項目中使用
pom文件中引用
<!--使用本地jar-->
<dependency>
<groupId>testJar</groupId>
<artifactId>junit</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/testJar-1.0.jar</systemPath>
</dependency>
App代碼修改,
package com.study;
import com.company.Mp;
import net.sf.json.JSONObject;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
JSONObject json = new JSONObject();
json.put("name","子落");
System.out.println(json.toString());
Mp.println("子落.");
}
}
這裏打包時採用將所有本地jar也一起打包到一個可執行jar中,這樣可以直接通過 java - jar xxx.jar運行。也可以通過先將本地jar註冊到maven倉庫,然後再打包,或者將本地jar複製到lib文件夾,然後通過在Manifest文件class-path中進行引用,這裏主要講,將所有依賴打包到一個jar中。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.study.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>com.jolira</groupId>
<artifactId>onejar-maven-plugin</artifactId>
<version>1.4.4</version>
<executions>
<execution>
<configuration>
<attachToBuild>true</attachToBuild>
<classifier>onejar</classifier>
</configuration>
<goals>
<goal>one-jar</goal>
</goals>
</execution>
</executions>
</plugin>
最終pom文件如下,
<?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.study</groupId>
<artifactId>mvnstudy</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>mvnstudy</name>
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--使用maven依賴查找 json-lib -->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
<!--使用本地jar-->
<dependency>
<groupId>testJar</groupId>
<artifactId>junit</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/testJar-1.0.jar</systemPath>
</dependency>
</dependencies>
<build>
<plugins>
<!--情景一,制定程序入口即可。-->
<!--<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.study.App</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>-->
<!--情景二,使打包的jar包含lib文件夾中的本地jar-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.study.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>com.jolira</groupId>
<artifactId>onejar-maven-plugin</artifactId>
<version>1.4.4</version>
<executions>
<execution>
<configuration>
<attachToBuild>true</attachToBuild>
<classifier>onejar</classifier>
</configuration>
<goals>
<goal>one-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
運行效果如下,
最終編譯的文件如下
其中 mvnstudy-1.0-SNAPSHOT.one-jar.jar 既可直接運行的jar,裏面包括之前引用的本地 testJar-1.0.jar 如下圖,
可以看到以上兩種方式,最終編譯的jar內部文件是不一樣的,第一種是將引用依賴中jar對應的文件解壓,然後編譯到最終的jar中,第二種是將引用的jar文件直接拿過來放入lib文件夾中,然後以套一層殼的方式運行,並不會對原jar文件進行解壓,具體使用哪種方式,還要看實際使用情況。
https://www.cnblogs.com/01x2v3/p/9000292.html
java項目打包成可執行jar用log4j將日誌寫在jar所在目錄
開發一個demo時想將日誌輸出到最終打包的jar所在目錄,從網上學習實驗整理之後的配置如下,
log4j.properties
log4j.rootLogger = INFO,console,logFile
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Threshold=INFO
log4j.appender.console.ImmediateFlush=true
log4j.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%p] %m%n
log4j.appender.logFile=org.apache.log4j.FileAppender
log4j.appender.logFile.ImmediateFlush=true
log4j.appender.logFile.Append=false
log4j.appender.logFile.Encoding=UTF-8
log4j.appender.logFile.File=jarDemo.log
log4j.appender.logFile.layout=org.apache.log4j.PatternLayout
log4j.appender.logFile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%p] %m%n
Main.java
private static Logger logger = LoggerFactory.getLogger(Main.class);
static {
String path = new File("").getAbsolutePath();
FileAppender appender = (FileAppender) org.apache.log4j.Logger.getRootLogger().getAppender("logFile");
appender.setFile(path + File.separator + "jarDemo.log");
}
最終打包成jar執行時,將會在jar文件所在目錄生產一個jarDemo.log的日誌文件,如果時用idea運行,這個日誌文件會在源碼所在項目的根目錄。
https://www.cnblogs.com/01x2v3/p/9042431.html
<!--打包插件,jar包和依賴包分開,依賴包打包到lib下 測試可行-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.xxx.xxxService</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<!--打包,把依賴包打包到jar內 測試OK-->
<!--<plugin>-->
<!--<artifactId>maven-assembly-plugin</artifactId>-->
<!--<configuration>-->
<!--<descriptorRefs>-->
<!--<descriptorRef>jar-with-dependencies</descriptorRef>-->
<!--</descriptorRefs>-->
<!--<archive>-->
<!--<manifest>-->
<!--<mainClass>com.xxx.xxxService</mainClass>-->
<!--</manifest>-->
<!--</archive>-->
<!--</configuration>-->
<!--<executions>-->
<!--<execution>-->
<!--<id>make-assembly</id>-->
<!--<phase>package</phase>-->
<!--<goals>-->
<!--<goal>single</goal>-->
<!--</goals>-->
<!--</execution>-->
<!--</executions>-->
<!--</plugin>-->
1.將依賴與自己的代碼打入同一個jar包
只需在pom中添加如下plugin
在include 中添加需要的依賴,在exclude 中添加不需要的依賴
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<includes>
<include>asm:asm</include>
</includes>
</artifactSet>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class></Main-Class>
</manifestEntries>
</transformer>
</transformers>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>com.jason:location-lte-lusq</exclude>
<exclude>com.jason:location-total-lusq</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
2.將依賴與自己的代碼的分開
自己的代碼會單獨打成一個jar,依賴保存到設定目錄
首先在Pom中添加如下plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<!-- The configuration of the plugin -->
<configuration>
<!-- Configuration of the archiver -->
<archive>
<!--
生成的jar中,不要包含pom.xml和pom.properties這兩個文件
-->
<addMavenDescriptor>false</addMavenDescriptor>
<!-- Manifest specific configuration -->
<manifest>
<!--
是否要把第三方jar放到manifest的classpath中
-->
<addClasspath>true</addClasspath>
<!--
生成的manifest中classpath的前綴,因爲要把第三方jar放到lib目錄下,所以classpath的前綴是lib/
-->
<classpathPrefix>corelib/</classpathPrefix>
<!--
應用的main class
-->
<mainClass></mainClass>
</manifest>
</archive>
<!--
過濾掉不希望包含在jar中的文件
-->
<excludes>
<exclude>${project.basedir}/xml/*</exclude>
</excludes>
</configuration>
</plugin>
<!-- The configuration of maven-assembly-plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<!-- The configuration of the plugin -->
<configuration>
<!-- Specifies the configuration file of the assembly plugin -->
<descriptors>
<descriptor>src/main/assembly/package.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
然後在src/main 下建立assembly 目錄,在assembly 中創建文件assembly.xml ,內容如下
<assembly>
<id>bin</id>
<!-- 最終打包成一個用於發佈的zip文件 -->
<formats>
<format>zip</format>
</formats>
<!-- Adds dependencies to zip package under lib directory -->
<dependencySets>
<dependencySet>
<!--
不使用項目的artifact,第三方jar不要解壓,打包進zip文件的lib目錄
-->
<useProjectArtifact>false</useProjectArtifact>
<outputDirectory>corelib</outputDirectory>
<unpack>false</unpack>
<!-- 紙漿scope 爲 compile 的打進jar-->
<scope>compile</scope>
</dependencySet>
</dependencySets>
<fileSets>
<!-- 把項目相關的說明文件,打包進zip文件的根目錄 -->
<fileSet>
<directory>${project.basedir}</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>README*</include>
<include>LICENSE*</include>
<include>NOTICE*</include>
</includes>
</fileSet>
<!-- 把項目的配置文件,打包進zip文件的config目錄 -->
<fileSet>
<directory>${project.basedir}\src\main\config</directory>
<outputDirectory>config</outputDirectory>
<includes>
<include>*.xml</include>
<include>*.properties</include>
</includes>
</fileSet>
<!-- 把項目的腳本文件目錄( src/main/scripts )中的啓動腳本文件,打包進zip文件的跟目錄 -->
<fileSet>
<directory>${project.build.scriptSourceDirectory}</directory>
<outputDirectory></outputDirectory>
<includes>
<include>startup.*</include>
</includes>
</fileSet>
<!-- 把項目的腳本文件(除了啓動腳本文件),打包進zip文件的script目錄 -->
<fileSet>
<directory>${project.build.scriptSourceDirectory}</directory>
<outputDirectory></outputDirectory>
<includes>
<include>startup.*</include>
</includes>
</fileSet>
<!-- 把項目自己編譯出來的jar文件,打包進zip文件的根目錄 -->
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory></outputDirectory>
<includes>
<include>a.jar</include>
</includes>
</fileSet>
</fileSets>
</assembly>
依賴會被打入corelib目錄中