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进行本地