maven小节,Nexus私服,构件打包发布,动态资源过滤,自动部署到本地或远程服务器...
生活随笔
收集整理的這篇文章主要介紹了
maven小节,Nexus私服,构件打包发布,动态资源过滤,自动部署到本地或远程服务器...
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
為什么80%的碼農都做不了架構師?>>> ??
閱讀此文的前提,對Maven 有一定了解,熟悉pom文件基礎
1:Nexus 建立私服 去下載nexus的war包格式的,最新版本的要使用高版本的web容器; 如:,下載后直接放到tomcat下 ,啟動運行
登陸進去可以看到默認有多個倉庫了手動建立倉庫 ,倉庫分類有1:宿主倉庫 2:代理倉庫 3:倉庫組 關于建立私服,也很簡單,不會的 推薦區看《Maven實戰》
這里是我創建的 自己的倉庫,也包含有默認倉庫
2:pom.xml配置
<!-- lang: java --> <!-- 為此項目配置倉庫(repositories)和插件倉庫(pluginRepositories),這里配置Nexus私服倉庫,配置在pom中,表示只在當前項目中有效 。在實際應用中,我們往往通過配置setting.xml使本機所有Maven項目都是用指定的私服 --> <repositories><repository><id>dy_nexus_group</id><name>dy_nexus_group</name><url>http://localhost:8080/nexus/content/groups/dy_nexus_group</url><releases><enabled>true</enabled></releases><snapshots><enabled>true</enabled></snapshots></repository> </repositories> <pluginRepositories><pluginRepository><id>dy_nexus_group</id><name>dy_nexus_group</name><url>http://localhost:8080/nexus/content/groups/dy_nexus_group</url><releases><enabled>true</enabled></releases><snapshots><enabled>true</enabled></snapshots></pluginRepository> </pluginRepositories> <!-- 發布構件 --> <!-- Nexus的倉庫對于匿名用戶是只讀的,為了能夠部署構件,還需要在setting.xml配置認證信息 --> <distributionManagement><repository><id>dy_nexus_hosted_release</id><name>dy_nexus_hosted_release</name><url>http://localhost:8080/nexus/content/repositories/dy_nexus_hosted_release</url></repository><snapshotRepository><id>dy_nexus_hosted_snapshot</id><name>dy_nexus_hosted_snapshot</name><url>http://localhost:8080/nexus/content/repositories/dy_nexus_hosted_snapshot</url></snapshotRepository> </distributionManagement><!-- Maven屬性 :包括內置屬性,pom屬性,setting屬性,自定義屬性,java系統屬性,環境變量屬性,參見p.280--> <properties><springframework.version>3.2.8</springframework.version> </properties><!-- 資源過濾 --> <profiles><!-- 針對開發環境的數據庫設置 (開發環境)clean compile install -Pdy.dev --><!-- spring配置中通過${db.driver}就可以拿到此處的配置值了 --><!-- Profile激活有多種方式詳見:p.285 --><profile><id>dy.dev</id><properties><myprop>dy.dev</myprop><!-- <db.driver>com.mysql.jdbc.Driver</db.driver><db.url>jdbc:mysql://127.0.0.3306/dev</db.url><db.username>pom.dev</db.username><db.password>pom.dev</db.password><db.maxIdle>11</db.maxIdle><db.maxActive>111</db.maxActive> --></properties><!-- 默認自動激活 --><activation><activeByDefault>true</activeByDefault></activation></profile><!-- 針對開發環境的數據庫設置 (開發環境)clean compile install -Pdy.test --><profile><id>dy.test</id><properties><myprop>dy.test</myprop></properties></profile><!-- 針對開發環境的數據庫設置 (開發環境)clean compile install -Pdy.prod --><profile><id>dy.prod</id><properties><myprop>dy.prod</myprop></properties></profile> </profiles>配置依賴: <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.7</version> <scope>test</scope> </dependency>
<dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${springframework.version}.RELEASE</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version><scope>provided</scope></dependency> </dependencies>構建項目:
<!-- 打包和發布的最終名字--> <finalName>hello-world</finalName> <!-- 配置過濾自定義的資源文件 --> <filters><filter>${project.basedir}/${myprop}.properties</filter> </filters> <!-- 為主資源目錄開啟過濾--> <resources><resource><directory>${project.basedir}/src/main/resources</directory><filtering>true</filtering></resource> </resources> <testResources><testResource><directory>${project.basedir}/src/test/resources</directory><filtering>true</filtering></testResource> </testResources> <!-- Cargo是一組幫助用戶操作web容器的工具。其提供兩種本地部署的方式:standalone模式和existing模式。 --><!-- standalone模式中:Cargo會從web容器的安裝目錄復制一份配置到用戶指定的目錄,然后在此基礎上部署應用。每次重新構建的時候,這個目錄被清空,所有配置重新清空。--><!-- existing模式中:用戶需要指定現有web容器的配置目錄,然后Cargo會直接使用這些配置并將應用部署到其 對應的目錄。運行命令:cargo:run(需要配置pluginGroups 詳見setting.xml)--><!-- 更多設置可參見官網: http://cargo.codehaus.org --> <plugin><groupId>org.codehaus.cargo</groupId><artifactId>cargo-maven2-plugin</artifactId><version>1.2.4</version><configuration><container><containerId>tomcat7x</containerId><home>F:/apache-maven-3.0.5-test/apache-tomcat-7.0.33</home></container><!-- <configuration><type>standalone</type><home>${project.build.directory}/tomcat7x</home><properties><cargo.servlet.port>8081</cargo.servlet.port></properties></configuration> --><configuration><type>existing</type><home>F:/apache-maven-3.0.5-test/apache-tomcat-7.0.33</home></configuration></configuration> <!-- lang: java --><!-- 部署至遠程服務器,前提是擁有該容器的相應管理員權限,配置如下: -->
<configuration><container><containerId>tomcat7x</containerId><type>remote</type></container><configuration><!--遠程正在運行的服務器 --><type>runtime</type><properties><cargo.hostname>192.168.168.50</cargo.hostname><cargo.servlet.uriencoding>UTF-8</cargo.servlet.uriencoding><cargo.remote.username>dengyang</cargo.remote.username><cargo.remote.password>dengyang</cargo.remote.password><cargo.tomcat.manager.url>http://192.168.168.50:8080/manager</cargo.tomcat.manager.url></properties></configuration></configuration></plugin>...... tomcat7.x 的tomcat-user.xml 配置管理員賬戶(tomcat6配置還不太一樣) 如下:
<!-- lang: js --> <role rolename="manager"/> <role rolename="manager-gui"/> <role rolename="manager-script"/> <role rolename="admin"/> <role rolename="admin-gui"/> <role rolename="admin-script"/> <!-- lang: xml --> <user username="dengyang" password="dengyang" roles="admin,admin-gui,admin-script,manager,manager- script,manager-gui"/>3:setting.xml配置
<!-- lang: java --> <localRepository>F:\JAVA_TOOLS\TOOLS\Maven\local_maven_repository</localRepository>本地倉庫地址 <servers><!-- Nexus的倉庫對于匿名用戶是只讀的,為了能夠部署構件,還需要在setting.xml配置認證信息, <server>中的id 和<repository>中的id完全一致 --> <server><id>dy_nexus_hosted_release</id><username>admin</username><password>admin123</password> </server> <server><id>dy_nexus_hosted_snapshot</id><username>admin</username><password>admin123</password> </server><!-- Another sample, using keys to authenticate.<server> <id>siteServer</id> <privateKey>/path/to/private/key</privateKey><passphrase>optional; leave empty if not used.</passphrase> </server> --> </servers> <!-- lang: java -->以下配置說明:本機所有maven項目私服配置倉庫(repositories)和插件倉庫(pluginRepositories),這樣配置除了會去私服nexus下載構件外,還會不時地訪問中央倉庫, 但我們希望的是所有構件和依賴下載的請求都僅僅通過Nexus,以全面發揮私服的作用。 這時候就需要配置Maven鏡像了。 <profile> <id>my_nexus</id> <repositories><repository><id>dy_nexus_group</id><name>dy_nexus_group</name><url>http://localhost:8080/nexus/content/groups/dy_nexus_group</url><releases><enabled>true</enabled></releases><snapshots><enabled>true</enabled></snapshots><layout>default</layout></repository> </repositories> <pluginRepositories><pluginRepository><id>dy_nexus_group</id><name>dy_nexus_group</name><url>http://localhost:8080/nexus/content/groups/dy_nexus_group</url><releases><enabled>true</enabled></releases><snapshots><enabled>true</enabled></snapshots></pluginRepository> </pluginRepositories> </profile> -->去mirrors下,增加配置,并修改此處配置: <!--如果倉庫X可以提供倉庫Y存儲的所有內容,那么就可以認為X是Y的一個鏡像。 dy_nexus_group倉庫組中增加central代理倉庫以及其他你自己的倉庫--><mirrors> <mirror><id>my_nexus</id><mirrorOf>*</mirrorOf><name>maven mirror for my nexus</name><url>http://localhost:8080/nexus/content/groups/dy_nexus_group</url> </mirror> </mirrors><!-- 修改以上profile配置如下:修改后配置,即使用maven鏡像配置,這里同時配置了插件倉庫,用不到也可以不配置; 倉庫和插件倉庫id都為central意味覆蓋了超級pom的中央倉庫的配置 --><profile> <id>my_nexus</id> <repositories><repository><id>central</id><name>central</name><url>http://central</url><releases><enabled>true</enabled></releases><snapshots><enabled>true</enabled></snapshots><layout>default</layout></repository> </repositories> <pluginRepositories><pluginRepository><id>central</id><url>http://central</url><releases><enabled>true</enabled></releases><snapshots><enabled>true</enabled></snapshots></pluginRepository> </pluginRepositories> </profile><!-- 使用activeProfiles 將my_nexus私服激活 , activeProfile 對應profile 中的id,使用maven鏡像的時候 此處對應的是mirror中的id--><activeProfiles> <activeProfile>my_nexus</activeProfile> </activeProfiles><!-- lang: xml -->使用cargo命令配置: <pluginGroups> <pluginGroup>org.codehaus.cargo</pluginGroup> </pluginGroups>此文主要用于個人總結,可能有很多人看不明白,可以聯系我dyyweb@163.com,也可以問度娘!轉載于:https://my.oschina.net/dyyweb/blog/227189
總結
以上是生活随笔為你收集整理的maven小节,Nexus私服,构件打包发布,动态资源过滤,自动部署到本地或远程服务器...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 12款界面精美的 HTML5 CSS3
- 下一篇: android的NDK和java进行本地