Maven 變量及常見插件配置詳解


Maven 的 pom.xml 常用 變量 插件 配置 詳解

一、變量 - 自定義變量及內置變量

1. 自定義變量

<properties>
	<project.build.name>tools</project.build.name>
	<project.build.sourceencoding>UTF-8</project.build.sourceencoding>
</properties>

2. 內置變量

${basedir} 項目根目錄    
${project.build.directory} 構建目錄,缺省為target    
${project.build.outputDirectory} 構建過程輸出目錄,缺省為target/classes    
${project.build.finalName} 產出物名稱,缺省為${project.artifactId}-${project.version}    
${project.packaging} 打包類型,缺省為jar    
${project.xxx} 當前pom文件的任意節點的內容

二、常見插件配置

1. 編譯插件

<plugin>
	<groupid>org.apache.maven.plugins</groupid>
	<artifactid>maven-compiler-plugin</artifactid>
	<configuration>
		<source>1.6</source>
		<target>1.6</target>
		<encoding>${project.build.sourceEncoding}</encoding>
	</configuration>
</plugin>
  • source: 源代碼編譯版本
  • target: 目標平台編譯版本
  • encoding: 字符集編碼

2. 設置資源文件的編碼方式

<plugin>
	<groupid>org.apache.maven.plugins</groupid>
	<artifactid>maven-resources-plugin</artifactid>
	<version>2.4.3</version>
	<executions>
		<execution>
			<phase>compile</phase>
		</execution>
	</executions>
	<configuration>
		<encoding>${project.build.sourceEncoding}</encoding>
	</configuration>
</plugin>

xml、properties 文件都是資源文件,編碼的時候遇到中文總要進行轉碼!用什么編碼?UTF-8,那就記得強制 <encoding>${project.build.sourceEncoding}</encoding>

3. 自動拷貝 jar 包到 target 目錄

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-dependency-plugin</artifactId>
	<version>2.6</version>
	<executions>
		<execution>
			<id>copy-dependencies</id>
			<phase>compile</phase>
			<goals>
				<goal>copy-dependencies</goal>
			</goals>
			<configuration>
				<!-- ${project.build.directory}為Maven內置變量,缺省為target -->
				<outputDirectory>${project.build.directory}/lib</outputDirectory><!-- 表示是否不包含間接依賴的包 -->
				<excludeTransitive>false</excludeTransitive><!-- 表示復制的jar文件去掉版本信息 -->
				<stripVersion>true</stripVersion>
			</configuration>
		</execution>
	</executions>
</plugin>   

關於 maven-dependency-plugin:用得最多的幾個操作: copy , copy-dependencies 和它們對應的 unpack , unpack-dependencies

描述:copy 和 unpack 操作是由要拷某個包,這個包需要具體指定要拷哪個包,與當前工程的依賴沒有關系。這兩者區別 - 是否解壓

copy-dependenciesunpack-dependencies ,但是它是用來拷當前工程的依賴包的。這兩者區別 - 是否解壓

參考鏈接:http://liugang594.iteye.com/blog/2093082

4. 生成源代碼 jar 包

<plugin>
	<artifactId>maven-source-plugin</artifactId>
	<version>2.1</version>
	<configuration>
		<!-- <finalName>${project.build.name}</finalName> -->
		<attach>true</attach>
		<encoding>${project.build.sourceEncoding}</encoding>
	</configuration>
	<executions>
		<execution>
			<phase>compile</phase>
			<goals>
				<goal>jar</goal>
			</goals>
		</execution>
	</executions>
</plugin>

5. 將項目打成 jar 包

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-jar-plugin</artifactId>
	<version>2.4</version>
	<configuration>
		<archive>
			<manifest>
				<!-- 告知 maven-jar-plugin添加一個 Class-Path元素到 MANIFEST.MF文件,以及在Class-Path元素中包括所有依賴項 -->
				<addClasspath>true</addClasspath><!-- 所有的依賴項應該位於 lib文件夾 -->
				<classpathPrefix>lib/</classpathPrefix><!-- 當用戶使用 lib命令執行JAR文件時,使用該元素定義將要執行的類名 -->
				<mainClass>com.zhengtian.tools.service.phone.MobilePhoneTool</mainClass>
			</manifest>
		</archive>
	</configuration>
</plugin>

在將項目打成 jar 包時,有時會需要將項目打成可以直接運行的 jar 包,因此就需要將項目依賴的 jar 包也打入 jar 包中,此時需要在 Eclipse 上安裝例外一個插件,用來打可執行 jar 包,詳情見鏈接 http://zheng12tian.iteye.com/blog/1765626

更多 maven 配置詳見:https://my.oschina.net/zh119893/blog/276090

關於 maven-assembly-plugin 的使用

“assembly” 是把一組文件、目錄、依賴元素組裝成一個歸檔文件

參考鏈接:http://blog.csdn.net/WANGYAN9110/article/details/38646677

pom 中配置

<plugin>
	<artifactId>maven-assembly-plugin</artifactId>
	<version>2.4.1</version>
	<executions>
		<execution>
			<id>make-zip</id><!-- 綁定到package生命周期階段上 -->
			<phase>package</phase>
			<goals>
				<!-- 綁定到package生命周期階段上 -->
				<goal>single</goal>
			</goals>
			<configuration>
				<descriptors>
					<!--描述文件路徑-->
					<descriptor>src/assembly/assembly.xml</descriptor>
				</descriptors>
			</configuration>
		</execution>
	</executions>
</plugin>

assembly.xml 配置

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
	<id>distribution</id>
	<formats>
		<format>zip</format>
	</formats>
	<fileSets>
		<fileSet>
			<directory>${project.basedir}\src\main\resources</directory>
			<outputDirectory>\</outputDirectory>
		</fileSet>
		<fileSet>
			<directory>${project.basedir}\src\bin</directory>
			<outputDirectory>\bin</outputDirectory>
		</fileSet>
	</fileSets>
	<dependencySets>
		<dependencySet>
			<useProjectArtifact>true</useProjectArtifact>
			<outputDirectory>lib</outputDirectory><!-- 將scope為runtime的依賴包打包到lib目錄下。 -->
			<scope>runtime</scope>
		</dependencySet>
	</dependencySets>
</assembly>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM