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

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

1:Nexus 建立私服

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

 登陆进去可以看到默认有多个仓库了

在此输入图片描述

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

这里是我创建的 自己的仓库,也包含有默认仓库
maven小节,Nexus私服,构件打包发布,动态资源过滤,自动部署到本地或远程服务器_第1张图片

2:pom.xml配置


<!-- 为此项目配置仓库(repositories)和插件仓库(pluginRepositories),
    这里配置Nexus私服仓库,配置在pom中,表示只在当前项目中有效 。
    在实际应用中,我们往往通过配置setting.xml使本机所有Maven项目都是用指定的私服 -->

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

配置依赖:

    <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配置


 <localRepository>F:\JAVA_TOOLS\TOOLS\Maven\local_maven_repository</localRepository>  本地仓库地址

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

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


      以下配置说明:本机所有maven项目私服配置仓库(repositories)和插件仓库(pluginRepositories),

这样配置除了会去私服nexus下载构件外,还会不时地访问中央仓库,

但我们希望的是所有构件和依赖下载的请求都仅仅通过Nexus,以全面发挥私服的作用。
这时候就需要配置Maven镜像了。 去mirrors下,增加配置,并修改此处配置:
<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> -->
<!-- 修改后配置,即使用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>

<activeProfile>my_nexus</activeProfile>


使用cargo命令配置:

<pluginGroups><pluginGroup>org.codehaus.cargo</pluginGroup></pluginGroups>

此文主要用于个人总结,可能有很多人看不明白,可以联系我[email protected],也可以问度娘!

你可能感兴趣的:(java,maven,nexus)