因为今天一个朋友学习过程中用到了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目录中