因項目須要,須要在一開源項目上作二 次開發,對開發後的項目須要再構建出本身的安裝包(如Linux下的*.tar.gz,Windows下的*.zip格式)。從網上搜索資料發現maven插件能夠實現這個需求。現把已經驗證成功的maven的插件配置pom文件分享。以備有共同須要的人能夠參考,同時給本身作個備忘:java
pom.xml內容以下:
apache
<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>xxxx</groupId> <artifactId>xxxxx</artifactId> <version>3.4.6</version> <name>xxxx</name> <description>xxxxx</description> <properties> <target.dir>target</target.dir> </properties> <dependencies> <dependency> .... </dependency> </dependencies> <build> <plugins> <!-- 指定多個源代碼目錄、多個資源文件目錄 --> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <version>1.8</version> <executions> <execution> <id>add-source</id> <phase>generate-sources</phase> <goals> <goal>add-source</goal> </goals> <configuration> <sources> <source>src/java/main</source> <source>src/java/generated</source> </sources> </configuration> </execution> </executions> </plugin> <!-- 編譯插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.0</version> <configuration> <source>1.6</source> <target>1.6</target> <encoding>UTF8</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.6</version> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> <!-- 生成源文件jar包文件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>2.1.1</version> <executions> <execution> <id>attach-sources</id> <goals> <goal>jar-no-fork</goal> </goals> </execution> </executions> </plugin> <!-- 打字節碼包插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.4</version> <configuration> <includes> <classesDirectory>org/</classesDirectory> </includes> <executions> <execution> <id>make-a-jar</id> <phase>compile</phase> <goals> <goal>jar</goal> </goals> </execution> </executions> </configuration> </plugin> <!-- 部署插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.4</version> <configuration> <descriptors> <descriptor>assembly.xml</descriptor> </descriptors> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
其中,assembly.xml是部署插件maven-assembly-plugin的配置文件,在assembly.xml裏指定哪些資源須要部署到安裝包中。其內容以下:maven
<assembly 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/assembly-2.2.xsd"> <id>full</id> <formats> <format>tar.gz</format> <!--Linux系統下打包文件格式 --> <format>zip</format> <!--Windows系統下打包文件格式 --> </formats> <includeBaseDirectory>true</includeBaseDirectory> <dependencySets> <dependencySet> <outputDirectory>lib</outputDirectory><!-- 將scope爲runtime的依賴包打包到lib目錄下。 --> <scope>runtime</scope> <useProjectArtifact>false</useProjectArtifact> </dependencySet> </dependencySets> <fileSets> <fileSet> <outputDirectory>/</outputDirectory> <directory>/</directory> <includes> <include>bin/*</include> <include>conf/*</include> <include>*.xml</include> <include>*.txt</include> </includes> <excludes> <exclude>pom.xml</exclude> <exclude>build.xml</exclude> <exclude>build_bak.xml</exclude> <exclude>assembly.xml</exclude> </excludes> </fileSet> <fileSet> <directory>/target/</directory> <outputDirectory>/</outputDirectory> <includes> <include>*.jar</include> </includes> </fileSet> </fileSets> </assembly>