在完成模塊開發后,需要發布jar到nexus上,與此同時,則要部署開發的模塊,需要將編譯打包的jar復制到指定的路徑,再進行部署,而不是手動的去復制那些jar,因為當模塊多的話,則會感到特別的煩,所以,利用maven集成的ant來進行這些操作。pom.xml文件配置如下:
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <target.dir>F:\生產發布包\發布包</target.dir> <target.version>2.1.4-1.0-RELEASE</target.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.3</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> <plugin> <artifactId>maven-source-plugin</artifactId> <version>2.1</version> <configuration> <attach>true</attach> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-antrun-plugin</artifactId> <executions> <execution> <phase>package</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <mkdir dir="${target.dir}/${target.version}"/> <copy todir="${target.dir}/${target.version}" overwrite="true" > <fileset dir="${project.build.directory}" erroronmissingdir="false"> <include name="*.jar"/> </fileset> </copy> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> <distributionManagement> <snapshotRepository> <id>snapshots</id> <name>meis</name> <url>http://191.168.2.1:8081/repository/maven-snapshots/</url> <uniqueVersion>true</uniqueVersion> </snapshotRepository> <repository> <id>releases</id> <name>meis</name> <url>http://191.168.2.1:8081/repository/maven-releases/</url> </repository> </distributionManagement>
其中,plugin的artifactId為maven-antrun-plugin的就是Ant插件,tasks的內容可參考Ant的幫助文檔,查閱 Ant Tasks;plugin的artifactId為maven-source-plugin的則表示在打包的時候同時打包源碼,編譯后的jar和源碼jar都會被上傳至nexus上面;關於nexus的登錄名和密碼,需要在maven的setting.xml中配置:
<server> <id>releases</id> <username>admin</username> <password>admin123</password> </server> <server> <id>snapshots</id> <username>admin</username> <password>admin123</password> </server>
需要注意id保持一致;
關於上面copy任務的解說:
1.mkdir為創建對應的文件夾,如已存在則忽略;
2.copy的todir為復制文件的目的地,overwrite為強制覆蓋,默認為false;
3.fileset為文件集,dir為目錄,${project.build.directory}指的是當前構建路勁,指的是 項目/target 目錄,erroronmissingdir為false指的是當目錄不存在時忽略掉,默認為拋出異常;
4.include表示文件包含*.jar的文件,指復制*.jar文件,編譯的jar和源碼jar;
復制任務需設置phase為package,否則jar沒有打包就會執行復制任務。
右鍵運行(Run As)--》Maven build...--》在Goals中輸入命令clean deploy,點擊Run,則會進行打包,此時可查看控制台信息。
如需重復部署(deploy),則需設置nexus上面release Repository的policy為Allow redeploy