Maven系列--在項目中的使用

開頭的時候說過,能接觸到maven,全賴boss的高明只見在項目中使用了。也虧得開明的boss甩給了我一週多的時間去了解和研究。程序員的行業,廢話再多也抵不過真槍實彈,如下介紹一下我在項目中是如何使用maven的。
程序員

這個maven項目是一個有三個子系統,分別是admin、index和search。
sql

圖1 [項目總結構圖]shell

咱們首先分析一下圖1:apache

  • build->存放構建生成的打包項目eclipse

  • core->存放核心代碼項目maven

  • doc->存放項目文檔的文件夾svn

  • modules->存放功能模塊項目ui

  • *.bat ->構建項目包的腳本
    google

其實,對maven來講,core或者modules中的項目均爲module,只不過在我這個項目中就按照功能劃分了一下而已。url

先來看一下目錄結構中的pom.xml,這個文件主要用來管理各個module

<?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>test.maven</groupId>
	<artifactId>test</artifactId>
	<version>2.1.60-SNAPSHOT</version>
	<packaging>pom</packaging>

	<profiles>
		<!-- 所有模塊 -->
		<profile>
			<id>all</id>
			<modules>
				<module>core/parent/pom.xml</module>
				<module>core/core/pom.xml</module>
				<module>core/wordsegment/pom.xml</module>
				<module>core/queryprocessor/pom.xml</module>
				<module>modules/index/pom.xml</module>
				<module>modules/search/pom.xml</module>
				<module>modules/admin/pom.xml</module>
			</modules>
		</profile>

		<!-- index -->
		<profile>
			<id>index</id>
			<modules>
				<module>core/parent/pom.xml</module>
				<module>core/core/pom.xml</module>
				<module>core/wordsegment/pom.xml</module>
				<module>modules/index/pom.xml</module>
			</modules>
		</profile>

		<!-- search -->
		<profile>
			<id>search</id>
			<modules>
				<module>core/parent/pom.xml</module>
				<module>core/core/pom.xml</module>
				<module>core/wordsegment/pom.xml</module>
				<module>core/queryprocessor/pom.xml</module>
				<module>modules/search/pom.xml</module>
			</modules>
		</profile>

		<!-- admin -->
		<profile>
			<id>admin</id>
			<modules>
				<module>core/parent/pom.xml</module>
				<module>core/core/pom.xml</module>
				<module>modules/admin/pom.xml</module>
			</modules>
		</profile>
	</profiles>
</project>

這個pom.xml有什麼用?其最主要的做用就是在構建打包時分類構建。好比我只想單獨構建admin模塊,那我就能夠雙擊運行build_admin.bat或者執行

mvn package -Padmin

 如此將不會影響到此打包過程當中沒使用到的module。

請注意<module>標記中使用到的是相對路徑。



接下來要講得是core/parent/pom.xml這個文件

圖2

咱們能夠看到,parent下只有一個pom.xml文件,這個文件管理着項目中使用到的全部公用依賴包以及插件,因爲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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>test.maven</groupId>
	<artifactId>test-parent</artifactId>
	<packaging>pom</packaging>

	<version>2.1.60-SNAPSHOT</version>

	<properties>
		<!-- 主要依賴庫的版本定義 -->
		<test.maven.version>${project.version}</test.maven.version>
		<!-- Plugin的屬性定義 -->
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<!-- 打包私服URL -->
	<distributionManagement>
		<repository>
			<id>releases</id>
			<url>http://192.168.8.181:9981/nexus/content/repositories/releases</url>
		</repository>
		<snapshotRepository>
			<id>snapshots</id>
			<url>http://192.168.8.181:9981/nexus/content/repositories/snapshots</url>
		</snapshotRepository>
	</distributionManagement>

	<!-- 預約義依賴項的version,scope與exclusions,子項目中只需定義groupId 和 artifactId 便可 -->
	<dependencyManagement>
		<dependencies>
			<!-- modules begin 項目中依賴到的其餘項目模塊 -->
			<dependency>
				<groupId>test.maven</groupId>
				<artifactId>core</artifactId>
				<version>${test.maven.version}</version>
			</dependency>
			
			<dependency>
				<groupId>test.maven</groupId>
				<artifactId>queryprocessor</artifactId>
				<version>${test.maven.version}</version>
			</dependency>

			....................................................................
			....................................................................
			....................................................................
			
			<!-- google -->
			<dependency>
				<groupId>com.google.guava</groupId>
				<artifactId>guava</artifactId>
				<version>15.0</version>
				<scope>compile</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<!-- 插件配置 -->
	<build>
		<pluginManagement>
			<plugins>
				<!-- compiler插件, 設定JDK版本 -->
				<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>${project.build.sourceEncoding}</encoding>
						<showWarnings>true</showWarnings>
					</configuration>
				</plugin>
                                
                                .........................................................
                                .........................................................
                                .........................................................

				<!-- eclipse插件,設定下載Source並屏蔽.svn文件 -->
				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-eclipse-plugin</artifactId>
					<version>2.9</version>
					<configuration>
						<sourceExcludes>
							<sourceExclude>**/.svn/</sourceExclude>
						</sourceExcludes>
						<downloadSources>true</downloadSources>
						<downloadJavadocs>false</downloadJavadocs>
					</configuration>
				</plugin>
			</plugins>
		</pluginManagement>
	</build>
</project>

<dependencyManagement>標記中管理了全部用到的依賴包,明肯定義了用到的依賴包的版本

<pluginManagement>中標記了用到的插件,併爲一些插件設定了部分統一配置,如maven-compiler-plugin插件中的JDK版本設定爲1.7

咱們再看module中是怎麼使用這個parent的pom.xml的,例如queryprocessor的pom.xml

<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>

	<!-- 集成parent的pom.xml -->
	<parent>
		<groupId>test.maven</groupId>
		<artifactId>test-parent</artifactId>
		<version>2.1.60-SNAPSHOT</version>
		<relativePath>../parent/pom.xml</relativePath>
	</parent>

	<artifactId>queryprocessor</artifactId>
	<packaging>jar</packaging>

	<dependencies>
	        <!-- 應用項目中的其餘項目,看見沒,這裏沒有寫版本號!由於parent中寫了 -->
		<dependency>
			<groupId>test.maven</groupId>
			<artifactId>wordsegment</artifactId>
		</dependency>


		<!-- lucene 引用依賴包也沒有寫版本號,parent中的依賴包管理已經寫好了-->
		<dependency>
			<groupId>org.apache.lucene</groupId>
			<artifactId>lucene-queries</artifactId>
		</dependency>

                ....................................................................
                ....................................................................
                ....................................................................
                
		<!-- test -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<finalName>test-${project.artifactId}-${project.version}</finalName>
		<plugins>
			<!-- compiler插件 這裏也沒有寫版本號,更沒有寫配置,這個項目使用的就是jdk1.7,maven3默認使用的是jdk1.6-->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
			</plugin>

			....................................................................
			....................................................................
			....................................................................

			<!-- dependency插件 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-dependency-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

從上面的pom.xml能夠看到,依賴包以及插件的配置中,已經再也不寫版本號了,插件中也能夠默認使用parent中的配置。


最後面講講怎麼把項目打包到build文件夾中

例如admin項目中,用到了打包插件maven-assembly-plugin

寫貼上admin項目的目錄結構圖

圖3

<!-- assembly插件 -->
<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-assembly-plugin</artifactId>
	<executions>
		<execution>
			<id>assembly:admin</id>
			<phase>package</phase> <!-- 綁定在打包階段 -->
			<goals>
				<goal>single</goal>
			</goals>
		</execution>
	</executions>
       	<configuration>
		<finalName>${project.artifactId}</finalName>
		<outputDirectory>target/assembly</outputDirectory> <!-- 打包後,目標輸出的文件夾 -->
		<filters>
			<filter>${basedir}/src/main/assemblies/filter.properties</filter> <!-- 存放須要替換的變量的值,好比執行腳本中有個${VersionID},根據filter裏的值能替換成對應的 -->
		</filters>
		<descriptors>
			<descriptor>src/main/assemblies/assembly.xml</descriptor> <!-- 打包的詳細配置文件 -->
		</descriptors>
	</configuration>
</plugin>


再來看看assembly.xml是怎麼處理的

<?xml version="1.0"?>
<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>dir</format><!-- 這裏是有多種打包方式的,經常使用的有:zip、tar、tar.gz、dir、war、jar -->
	</formats>
	<includeBaseDirectory>false</includeBaseDirectory>

	<!-- 須要裝配copy的文件 -->
	<fileSets>
		<!-- 說明文件 -->
		<fileSet>
			<directory>work</directory>
			<outputDirectory>/</outputDirectory>
			<filtered>false</filtered>
			<includes>
				<include>*.txt</include>
			</includes>
		</fileSet>
		<!-- 配置文件 -->
		<fileSet>
			<directory>work/conf</directory>
			<outputDirectory>/conf</outputDirectory>
			<filtered>true</filtered>
		</fileSet>
		<!-- database.sql文件 -->
		<fileSet>
			<directory>work/db</directory>
			<outputDirectory>/db</outputDirectory>
			<filtered>false</filtered>
		</fileSet>

		<!-- 日誌文件 -->
		<fileSet>
			<directory>work/logs</directory>
			<outputDirectory>/logs</outputDirectory>
			<filtered>false</filtered>
		</fileSet>
	</fileSets>
</assembly>

到這裏,構建出來的文件被放入到了target/assembly這個目錄中,剩下的就是build_admin.bat的工做了

@echo off
echo [info] 確保默認JDK版本爲JDK7.0及以上版本,已配置JAVA_HOME.

set MVN=mvn
set MAVEN_OPTS=%MAVEN_OPTS% -XX:MaxPermSize=128m

echo MAVEN命令:%MVN%
echo ---------------------------------------------------------------------------------
echo *                [Step 1] 開始編譯並打包admin工程            *
echo ---------------------------------------------------------------------------------

call %MVN%  clean install -Padmin -Dmaven.test.skip=true
if errorlevel 1 goto error

echo ---------------------------------------------------------------------------------
echo *      [Step 2]admin工程打包成功  ,放入對應的build目錄     *
echo ---------------------------------------------------------------------------------

set time_hh=%time:~0,2%
if /i %time_hh% LSS 10 (set time_hh=0%time:~1,1%)
set currentTime=%date:~,4%%date:~5,2%%date:~8,2%_%time_hh%%time:~3,2%%time:~6,2%
set filename=admin_%currentTime%

rem echo %filename%
echo 開始建立build目錄
md build\%filename%

xcopy modules\admin\target\admin.war build\%filename%\.
xcopy /e /s modules\admin\target\assembly\admin-distribution build\%filename%\. 

echo ---------------------------------------------------------------------------------
echo *              [Step 3] 開始清除操做                                           *
echo ---------------------------------------------------------------------------------

call %MVN% clean -Padmin -Dmaven.test.skip=true
if errorlevel 1 goto error1

echo ---------------------------------------------------------------------------------
echo *              		 清除操做成功                                             *
echo ---------------------------------------------------------------------------------

goto end

:error
echo "編譯打包失敗,有錯誤發生"
goto end
:error1
echo "清除失敗,有錯誤出現"
goto end
:end
pause

經過執行這腳本將target/assembly裏的文件搬運到build中,完整的構建包就出來了,而後就能夠交付給其餘部門使用了!

相關文章
相關標籤/搜索