1. 程式人生 > >Maven打包和Copy檔案功能

Maven打包和Copy檔案功能

專案結構:

POM.XML

<?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.scu.tgm.mavenpackge</groupId>
    <artifactId>test</artifactId>
    <version>1.1</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <pkg.name>
            <![CDATA[TGM&&Counter.pkg]]>
        </pkg.name>
    </properties>

    <build>
        <plugins>
            <!-- 使用assembly.xml打包 -->
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <appendAssemblyId>false</appendAssemblyId>
                    <descriptors>
                        <descriptor>src/main/resources/config/assembly.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>attached</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- copy檔案 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <tasks>
                                <!-- ${project.build.directory}這個是tartget目錄 -->
                                <!-- 這句話的意思是將${artifactId}-${version}.zip包裡面的內容複製到 -->
                                <copy overwrite="true" tofile="${project.build.directory}/${pkg.name}"
                                      file="${project.build.directory}/${artifactId}-${version}.zip"/>
                            </tasks>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.7</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-excelant</artifactId>
            <version>3.14</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.7</version>
        </dependency>
    </dependencies>

</project>

assembly.xml

<?xml version="1.0" encoding="UTF-8"?>
<assembly
	xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
	<id>1.0</id>
	<formats>
		<!-- 這個是將下面的所有內容打成zip包裡面的意思,生成檔案位置在target下 -->
		<!-- 生成的預設格式為${artifactId}-${version}.zip -->
		<format>zip</format>
	</formats>
	<includeBaseDirectory>false</includeBaseDirectory>
	<!-- 打包想打的目錄中的檔案 -->
	<fileSets>
		<fileSet>
			<directory>src/main/resources/model-conf</directory>
			<outputDirectory>/conf</outputDirectory>
		</fileSet>
		<fileSet>
			<directory>src/main/resources/probe-conf</directory>
			<outputDirectory>/conf</outputDirectory>
		</fileSet>
	</fileSets>
	<!-- 打包想打的檔案 -->
	<files>
		<file>
			<!-- ${project.build.directory}這個是target目錄 -->
			<!-- ${project.build.finalName}這個是該專案打的jar包名字,如果不確定就直接寫死實際的檔名-->
			<source>${project.build.directory}/${project.build.finalName}.jar</source>
			<outputDirectory>/</outputDirectory>
			<!--輸出的別名-->
			<destName>bie-ming-out.jar</destName>
		</file>
		<file>
			<!-- 路徑是與pom.xml為參照,平級的直接寫名字,如src,target等-->
			<source>keystore</source>
			<outputDirectory>/</outputDirectory>
			<destName>keystore-out</destName>
		</file>
		<file>
			<!-- 路徑是與pom.xml為參照,平級的直接寫名字,如src,target等-->
			<source>pom.xml</source>
			<outputDirectory>/</outputDirectory>
			<destName>pom-out.xml</destName>
		</file>
	</files>
	<!-- 打包想打的jar -->
	<dependencySets>
		<!-- 這個是打包自己的程式程式碼 -->
		<dependencySet>
			<outputDirectory>/</outputDirectory>
			<includes>
				<!-- 這個是pom中的groupId:artifactId:jar -->
				<include>com.scu.tgm.mavenpackge:test:jar</include>
				<!-- 即使該依賴設定成provided也可以被打出來 -->
				<include>org.apache.poi:poi:jar</include>
			</includes>
		</dependencySet>
		<!-- 這個是將依賴打包,如果不想打包某個依賴就在dependency中新增<scope>provided</scope> -->
		<dependencySet>
			<useProjectArtifact>false</useProjectArtifact>
			<outputDirectory>/lib</outputDirectory>
			<outputFileNameMapping>${artifact.artifactId}.${artifact.extension}</outputFileNameMapping>
			<unpack>false</unpack>
			<useTransitiveDependencies>false</useTransitiveDependencies>
			<!-- 打包執行時的jar -->
			<scope>runtime</scope>
		</dependencySet>
	</dependencySets>
</assembly>