當(dāng)構(gòu)建軟件時(shí),我們經(jīng)常需要?jiǎng)?chuàng)建一些庫(kù)來(lái)收集所有的函數(shù)和類在共同的一起,把編譯的庫(kù)文件建立路徑,使編譯器可以找到庫(kù)編譯代碼時(shí)。
在Java中,這通常意味著創(chuàng)建一個(gè)具有庫(kù)類的jar文件。
在Maven中,我們可以使用“mvn install"打包項(xiàng)目并自動(dòng)部署到本地存儲(chǔ)庫(kù)。
當(dāng)執(zhí)行“安裝"階段時(shí),將執(zhí)行其之前的所有階段,例如“驗(yàn)證",“編譯",“測(cè)試",“封裝",“積分測(cè)試",“驗(yàn)證"階段。 之后這些階段將執(zhí)行安裝階段。
以下代碼顯示了從maven生成的POM文件。
<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>com.java2s.ide</groupId> <artifactId>xmlFileEditor</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>xmlFileEditor</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
基于上面的pom.xml文件,當(dāng)執(zhí)行“mvn install"時(shí),它會(huì)將項(xiàng)目打包到j(luò)ar文件中并復(fù)制到本地存儲(chǔ)庫(kù)。
它總是建議運(yùn)行“干凈"和“安裝"在一起,以便你總是部署最新的項(xiàng)目到本地存儲(chǔ)庫(kù)。
mvn clean install
上述命令生成以下結(jié)果。
c:\mvn_test\xmlFileEditor>mvn clean install [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building xmlFileEditor 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ xmlFileEditor --- [INFO] Deleting c:\mvn_test\xmlFileEditor\target [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ xmlFileEditor --- [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory c:\mvn_test\xmlFileEditor\src\main\resources [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ xmlFileEditor --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent! [INFO] Compiling 1 source file to c:\mvn_test\xmlFileEditor\target\classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ xmlFileEditor --- [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory c:\mvn_test\xmlFileEditor\src\test\resources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ xmlFileEditor --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent! [INFO] Compiling 3 source files to c:\mvn_test\xmlFileEditor\target\test-classes [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ xmlFileEditor --- [INFO] Surefire report directory: c:\mvn_test\xmlFileEditor\target\surefire-reports ------------------------------------------------------- T E S T S ------------------------------------------------------- Running com.java2s.ide.AppTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec Running com.java2s.ide.TestApp1 Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec Running com.java2s.ide.TestApp2 Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec Results : Tests run: 3, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ xmlFileEditor --- [INFO] Building jar: c:\mvn_test\xmlFileEditor\target\xmlFileEditor-1.0-SNAPSHOT.jar [INFO] [INFO] --- maven-install-plugin:2.4:install (default-install) @ xmlFileEditor --- [INFO] Installing c:\mvn_test\xmlFileEditor\target\xmlFileEditor-1.0-SNAPSHOT.jar to C:\Users\abc\.m2\repository\com\java2s\ide\xmlFileEditor\1.0-SNAPSHOT\xmlFileEditor-1.0-SNAPSHOT.jar [INFO] Installing c:\mvn_test\xmlFileEditor\pom.xml to C:\Users\abc\.m2\repository\com\java2s\ide\xmlFileEditor\1.0-SNAPSHOT\xmlFileEditor-1.0-SNAPSHOT.pom [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 6.250 s [INFO] Finished at: 2014-11-22T10:27:10-08:00 [INFO] Final Memory: 27M/369M [INFO] ------------------------------------------------------------------------ c:\mvn_test\xmlFileEditor>
我們可以在我們的本地maven存儲(chǔ)庫(kù)中查看已安裝的jar文件。
將jar文件安裝到本地存儲(chǔ)庫(kù)之后我們通過(guò)在它們的pom.xml文件中聲明以下相關(guān)性標(biāo)記來(lái)訪問(wèn)我們部署的“jar"文件。
<dependency> <groupId>com.java2s.ide</groupId> <artifactId>xmlFileEditor</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
更多建議: