maven说明文档(二)

六、版本管理

a)1.0-SNAPSHOP 不稳定版本,表示 一直在更新push代码修复bug的过程可以被覆盖。

 

maven说明文档(二)
      如图物理系统依赖订单系统,订单系统修复bug,将jar包重新传入私服仓库,物流系统对订单系统的依赖没有变化,物流系统不会读到新的jar包(本地仓库不会实时刷新,reimport也不一定强制刷新)

     两种解决方法:1.将本地repository中的依赖删除

                               2.mvn clean package -U(不管本地有没有jar包,强制去远程拉一次)

      注意:发布线上的版本不能使用SNAPSHOT,要用RELEASE,版本不能被覆盖

      1.SNAPSHOT 允许重复更新的版本可以被覆盖

       2.使用-U去更新的时候会出问题

b)版本号格式:主版本号.次版本号.增量版本号 -<里程碑版本>     如:1.0.0-RELEASE

七、常用命令

a) complie     编译

b) clean     删除target/ 发布前要把target删除掉,发布前一定要clean

c)  test      运行项目中所有的testCase  如:junit/testNG

d) package  打包动作 打包依据<packaging/>

e) install   把项目install 到 local repository(本地仓库)

 f) deploy  把本地的jar发布到remote(远端) 如:私服

八、插件

  a)常用插件

     i:查看常用插件地址

     https://maven.apache.org/plugins/

     http://www.mojohaus.org/plugins.html

      ii: findbugs 插件  静态代码检查

      iii:versions 统一升级版本号

       <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>version-maven-plugin</artifactId>
            <version>2.3</version>
        </plugin>

        使用命令:mvn versions:set  -DnewVersion = 1.1

      iv:打包源代码

      v:assembly 打包zip、war

      vi: tomcat7

b)自定义插件

      maven自定义插件官网

      https://maven.apache.org/guides/plugin/guide-java-plugin-development.html

      i 新建项目

      ii 改变pom中的packing 改成maven-plugin <packaging>maven-plugin</packaging>。pom.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.gupao</groupId>
    <artifactId>gupao-plugin</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>maven-plugin</packaging>
    <dependencies>

        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-plugin-api</artifactId>
            <version>3.5.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.maven.plugin-tools</groupId>
            <artifactId>maven-plugin-annotations</artifactId>
            <version>3.5</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>


</project>

      iii extends AbstractMojo。构建插件的代码:

/*声明一下 name  也就是goal的name*/
@Mojo(name="gupao",defaultPhase = LifecyclePhase.PACKAGE)
public class GuPaoMojo extends AbstractMojo {
    
   //参数
    @Parameter
    private String msg;
    
    //参数
    @Parameter
    private List<String> options;

    public void execute() throws MojoExecutionException, MojoFailureException {
        System.out.println("gupao plugin!!!" + msg);
        System.out.println("gupao plugin!!!" + options);
    }


}

iii:将插件在其他项目中引入

maven说明文档(二)

       goal与phase的关系:phase 就只是一个名字而已 是一个框 可以在这个框里 做多个plugin goal 指的是运行插件是哪个 真正执行的插件。maven是有一个个插件构成的。

 iv: 使用命令mvn  install 将插件放到本地

 v:参数传递

maven说明文档(二)

 传入参数的命令:mvn install -Dargs=123

九、profile

   1.使用场景 test/dev/pro分环境打包

 <profiles>
       <profile>
           <id>dev</id>
           <properties>
               <!--设置一个变量 profiles.active = dev-->
               <profiles.active>dev</profiles.active>
           </properties>
           <activation>
               <!--默认打包使用dev-->
               <activeByDefault>true</activeByDefault>
           </activation>
       </profile>
        <profile>
            <id>test</id>
            <properties>
                <profiles.active>test</profiles.active>
            </properties>
        </profile>
        <profile>
            <id>pro</id>
            <properties>
                <!--设置一个变量 profiles.active = dev-->
                <profiles.active>pro</profiles.active>
            </properties>
        </profile>
    </profiles>

    <build>
        <resources>
            <resource>
                <directory>${basedir}/src/main/resources</directory>
                <excludes>
                    <exclude>conf/**</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/main/resources/conf/${profiles.active}</directory>
            </resource>
        </resources>
     </build>

    maven打包命令:  mvn clean install -P pro/test/dev(默认)   

maven说明文档(二)

2.setting.xml中的配置更换

<!--公司私服-->
     <profile>
		<id>dev</id>
		<repositories>
			<repository>
				<id>local-nexus</id>
				<url>http://192.168.6:8081/nexus/content/groups/public</url>
				<releases>
					<enable>true</enable>
				</releases>
				<snapshots>
					<enabled>true</enabled>
				</snapshots>
			</repositry>
		<repositories>
		<pluginRepositories>
			<pluginRepositorie>
				<id>plugin-local-mirror</id>
				<url>http://192.168.1.6:8081/nexus/content/groups/public</url>
			</pluginRepositorie>
		</pluginRepositories>
	 </profile>
	 <!--连接另外的服务-->
	  <profile>
		<id>external</id>
		<repositories>
			<repository>
				<id>local-nexus</id>
				<url>http://192.168.6:8081/nexus/content/groups/public</url>
				<releases>
					<enable>true</enable>
				</releases>
				<snapshots>
					<enabled>true</enabled>
				</snapshots>
			</repositry>
		<repositories>
		<pluginRepositories>
			<pluginRepositorie>
				<id>plugin-local-mirror</id>
				<url>http://192.168.1.6:8081/nexus/content/groups/public</url>
			</pluginRepositorie>
		</pluginRepositories>
	 </profile>
	 
  </profiles>
	
  <activeProfiles>
	   <!-- <activeProfile>external</activeProfile> -->
		<activeProfile>dev</activeProfile>
  </activeProfiles>

可以通过activProfiles 中的activeProfile进行切换。