由於項目需要,想自己建立骨架,便於大家在同一個骨架下開發,便決定自己寫骨架並發布到maven倉庫,寫好后網上查了好多都發布不成功,反復多次終於成功,總結兩個原因。
1.pom文件的distributionManagement節點下的releases和snapshots節點id一定要和.m2/setting.xml中的server節點的id一樣
2.m2/setting.xml中的server節點的用戶名密碼一定要正確,建議用deployment
3.在nexus的Repositories中的Releases、Snapshots的配置Deployment Policy控制是否可以重新覆蓋版本發布
4.一直沒成功的原因:發布骨架項目時,需要在骨架項目的pom文件添加distributionManagement節點
5.發布項目代碼到倉庫時候一定要指定plugin的jdk版本
下面說明如何發布骨架項目:
1.配置好maven\conf目錄下settings.xml文件的<servers>段
<servers> <server> <id>nexus-3rd</id> <username>deployment</username> <password>deployment123</password> </server> <server> <id>snapshots</id> <username>deployment</username> <password>deployment123</password> </server> <server> <id>releases</id> <username>deployment</username> <password>deployment123</password> </server> </servers>
2.在要生成archetype項目上點右鍵,運行方式 -> Maven clean,清理一下項目
3.配置項目pom文件插件
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-archetype-plugin</artifactId> <version>2.2</version> </plugin>
3.點擊Maven build...,在goal欄填寫archetype:create-from-project,並運行
4.生成成功后,打開項目目錄下的target\generated-sources\archetype\pom.xml文件,加入(如果發布項目的jar包,項目的pom文件也需要加入)
<distributionManagement> <repository> <id>releases</id> <name>bazaar releases</name> <url>http://127.0.0.1:8081/nexus/content/repositories/releases/</url> </repository> <snapshotRepository> <id>snapshots</id> <name>bazaar snapshots</name> <url>http://127.0.0.1:8081/nexus/content/repositories/snapshots/</url> </snapshotRepository> </distributionManagement>
5.打開系統控制台,進入項目目錄下的target\generated-sources\archetype\,運行mvn deploy
提示build success的話,你的archetype就上傳到服務器了,默認的artifactId就是原來項目的artifactId加上-archetype
最后,要生成腳本bat或shell
shell
mvn --version mvn archetype:generate -B \ -DarchetypeCatalog=http://127.0.0.1:8081/nexus/content/repositories/releases/ \ -DarchetypeGroupId=com.hpe.bazaar \ -DarchetypeArtifactId=bazaar-order-archetype \ -DarchetypeVersion=1.0.0 \ -DgroupId=com.hpe.bazaar.product \ -DartifactId=bazaar-product \ -Dversion=1.0-SNAPSHOT
bat
mvn --version mvn archetype:generate -B ^ -DarchetypeCatalog=http://127.0.0.1:8081/nexus/content/repositories/releases/ ^ -DarchetypeGroupId=com.hpe.bazaar ^ -DarchetypeArtifactId=bazaar-order-archetype ^ -DarchetypeVersion=1.0.0 ^ -DgroupId=com.hpe.bazaar.product ^ -DartifactId=bazaar-product ^ -Dversion=1.0-SNAPSHOT