背景
因为对ftp上传的控制编码有特殊要求,为了方便测试使用,快速开发一个ftp小工具。为了了解maven插件,没有用springboot
操作
1、使用Eclipse 新建maven工程, 使用maven-archetype-quickstart模板创建java项目。2个java文件加一个properties配置文件写完后目录结构如下:

2、执行mvn package 命令打包后目录结构如下。其中的ftp.jar 和lib目录是项目运行需要的目录文件

3、ftp.jar 包内目录,里面打包了properties文件,第三方jar包没有一起打包进去,在target目录的lib目录下。

META-INF的manifest.mf文件内容如下:
Manifest-Version: 1.0
Built-By: haohaifeng
Class-Path: lib/commons-net-3.6.jar lib/commons-lang3-3.8.1.jar
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_102
Main-Class: com.test.ftp.App
经验总结(没有参考文章依据):
Class-Path 和 Main-Class路径为maven-jar-plugin配置。该plugin主要作用就是配置manifest.mf文件。用来获取main方法类路径和第三方jar路径。至于maven打jar包,不配置这个插件也可以。
使用maven-dependency-plugin包后虽然可以把第三方依赖包打包到指定目录,但会把maven-jar-plugin打包的ftp.properties文件忽略打包,需要再单独配置resources 打包配置文件。
maven-assembly-plugin & maven-shade-plugin可以把自己写的Java文件和第三方引用打成一个Jar。
执行 java -jar ftp.jar 即可运行
记录完毕!
以下是pom.xml 相关配置
使用maven-jar-plugin + maven-dependency-plugin 配置如下:
参考1:http://maven.apache.org/plugins/maven-jar-plugin/
参考2:http://maven.apache.org/components/plugins/maven-dependency-plugin/usage.html
<build>
<finalName>ftp</finalName>
<resources>
<!-- 编译之后包含properties -->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<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}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.test.ftp.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
</plugins>
</build>
附:使用maven-assembly-plugin打包配置如下:
参考:http://maven.apache.org/components/plugins/maven-assembly-plugin/usage.html
<build>
<finalName>ftp</finalName>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.test.ftp.App</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
附:使用maven-shade-plugin打包配置如下:
参考:https://maven.apache.org/plugins/maven-shade-plugin/examples/executable-jar.html
<build>
<finalName>ftp</finalName>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.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.test.ftp.App</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
本文介绍如何使用Maven打包Java应用程序,包括使用maven-jar-plugin、maven-dependency-plugin等插件,以及如何配置资源文件和第三方依赖,实现单个可执行Jar包的创建。
564

被折叠的 条评论
为什么被折叠?



