下面小編就為大家帶來一篇java application maven專案打自訂zip套件實例(推薦)。小編覺得蠻不錯的,現在就分享給大家,也給大家做個參考。一起跟著小編過來看看吧
1.配置pom.xml文件,加入build節點##
<build> <!-- 输出的包名 --> <finalName>p2p</finalName> <sourceDirectory>src/main/java</sourceDirectory> <resources> <!-- 控制资源文件的拷贝(默认复制到classes目录,最后打进jar包) --> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <!-- 排除外置的配置文件(运行时注释上使IDE能读取到配置文件;打包时放开注释让配置文件外置方便修改) --> <excludes> <exclude>config.properties</exclude> </excludes> </resource> <!-- 配置文件外置的资源(存放到config目录,也是classpath路径,下面会配置) --> <resource> <directory>src/main/resources</directory> <includes> <include>config.properties</include> </includes> <targetPath>${project.build.directory}/config</targetPath> </resource> </resources> <plugins> <!-- 设置编译版本 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin> <!-- 打包jar文件时,配置manifest文件,加入lib包的jar依赖 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <!-- 清单文件,设置入口类和classpath --> <manifest> <mainClass>com.hdwang.Application</mainClass> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> </manifest> <!-- 给清单文件添加键值对,增加classpath路径,这里将config目录也设置为classpath路径 --> <manifestEntries> <Class-Path>config/</Class-Path> </manifestEntries> </archive> <classesDirectory> </classesDirectory> </configuration> </plugin> <!-- 拷贝依赖的jar包到lib目录 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy</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-resources-plugin</artifactId> <version>2.5</version> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> <!-- 自定义打zip包 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.2.1</version> <configuration> <descriptors> <descriptor>src/main/assembly/assembly.xml</descriptor> </descriptors> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
讀取配置檔案的java程式碼,在IDE運行時和打包後,程式碼都不用修改,因為設定檔總是能從classpath路徑中找到! ! !
工具包的maven資訊#
<dependency> <groupId>commons-configuration</groupId> <artifactId>commons-configuration</artifactId> <version>1.10</version> </dependency>
2.新maven- assembly-plugin外掛程式的設定檔assembly.xml,內容如下##<assembly>
<id>bin</id>
<formats>
<format>zip</format>
</formats>
<!-- 使用assembly拷贝依赖包 -->
<!--<dependencySets>-->
<!--<dependencySet>-->
<!--<!– 是否包含自己(将项目生成的jar包也输出到lib目录) –>-->
<!--<useProjectArtifact>false</useProjectArtifact>-->
<!--<outputDirectory>lib</outputDirectory>-->
<!--</dependencySet>-->
<!--</dependencySets>-->
<fileSets>
<!-- 从目标目录拷贝文件去压缩 -->
<fileSet>
<directory>target</directory>
<includes>
<include>*.jar</include>
</includes>
<outputDirectory>/</outputDirectory>
</fileSet>
<fileSet>
<directory>target/lib</directory>
<outputDirectory>/lib</outputDirectory>
</fileSet>
<fileSet>
<directory>target/config</directory>
<outputDirectory>/config</outputDirectory>
</fileSet>
<!-- 从源目录拷贝文件去压缩 -->
<fileSet>
<directory>src/main/run</directory>
<includes>
<include>*.sh</include>
<include>*.cmd</include>
</includes>
<outputDirectory>/</outputDirectory>
</fileSet>
<fileSet>
<directory>src/main</directory>
<includes>
<include>ReadMe.txt</include>
</includes>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
</assembly>
中運行,執行mvn package或mvn inst all便可觸發此外掛程式的執行。這裡我註解掉了拷貝依賴包的程式碼,是因為在pom.xml檔中已經配置了maven-dependency-plugin執行這樣的操作,無須重複配置。 fileSet可以設定需要拷貝壓縮的文件,directory路徑相對於專案根目錄,outputDirectory路徑相對於輸出目錄target,includes可以對拷貝的檔案進行篩選。這裡可以拷貝壓縮輸出目錄的文件,應該就是因為此插件運行在程式編譯打包之後,這樣子就達到了我們自訂打包的要求:編譯->拷貝資源檔案->專案打包->拷貝依賴的jar包-> assembly進行拷貝壓縮。然後使用打出的zip套件就可以去部署發布了,解壓縮後就能運作。
3.程式打包流程示意圖
#
以上是java application maven專案打自訂zip套件實例(必看)的詳細內容。更多資訊請關注PHP中文網其他相關文章!