maven小节,Nexus私服,构件打包发布,动态资源过滤,自动部署到本地或远程服务器

阅读此文的前提,对Maven 有必定了解,熟悉pom文件基础java

1:Nexus 创建私服 去下载nexus的war包格式的,最新版本的要使用高版本的web容器; 如:,下载后直接放到tomcat下 ,启动运行mysql

登录进去能够看到默认有多个仓库了

在此输入图片描述

手动创建仓库 ,仓库分类有1:宿主仓库 2:代理仓库 3:仓库组 关于创建私服,也很简单,不会的 推荐区看《Maven实战》web

这里是我建立的 本身的仓库,也包含有默认仓库 在此输入图片描述spring

2:pom.xml配置sql

<!-- 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>

构建项目:apache

<!-- 打包和发布的最终名字-->
<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 -->

<!-- 部署至远程服务器,前提是拥有该容器的相应管理员权限,配置以下: -->api

<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配置还不太同样) 以下:tomcat

<!-- 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,也能够问度娘!
相关文章
相关标签/搜索