maven有三种插件可以用来打包:
- maven-jar-plugin,默认的打包插件,用来打普通的project JAR包;
- maven-shade-plugin,用来打可执行JAR包,也就是所谓的fat JAR包;
- maven-assembly-plugin,支持自定义的打包结构,也可以定制依赖项等
现有maven的普通java项目,项目根路径下有files文件夹,该文件夹下有一些文件
1、利用resource插件将files文件夹编译到target/classes目录下
<build>
<resources>
<resource>
<directory>files</directory>
<targetPath>/files</targetPath>
</resource>
</resources>
</build>
2、利用maven-assembly-plugin插件将files文件夹以及项目依赖包一起打包
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.asiainfo.group.kafka_ui_client.ui.KafkaClientUI</mainClass>
</manifest>
</archive>
<!-- <finalName>kafka_ui_client-1.0</finalName> -->
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
<!-- <outputDirectory>E:/nomanager</outputDirectory> -->
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
3、assembly.xml
<?xml version='1.0' encoding='UTF-8'?>
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0
http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>make-assembly</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.build.directory}/classes</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
<dependencySets>
<!-- <dependencySet>
<unpack>false</unpack>
<scope>runtime</scope>
<outputDirectory>lib</outputDirectory>
<useProjectArtifact>false</useProjectArtifact>
</dependencySet> -->
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>runtime</scope>
</dependencySet>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>system</scope>
</dependencySet>
</dependencySets>
</assembly>
4、java如何读取jar包内部文件
//这个路径是jar包内的绝对路径,也是以target/classes作为根目录的绝对路径
InputStream in = ConsumerConfig.class.getResourceAsStream("/files/consumer.properties");