maven

作者: 我也有键盘 | 来源:发表于2018-12-27 17:24 被阅读0次

pom文件常见名词的意义

properties

定义父pom里出现的一些参数变量

dependencyManagement:

  • 最好只出现在父pom中
  • 统一版本号
  • 在dependencyManagement中声明的dependency,仅仅是一个声明,不会默认被子类引用;如果子类要引用需要自己再引

dependency

  1. type 默认jar

  2. scope

    • compile 编译,默认值

    • test 测试,测试jar,不会被打到最终war里

      如,将Junit依赖加上<scope>test</scope>

    • provided 用在编译期,不会被打包

    • runtime 运行时,编译时不需要,可以不去理会它

      如:JDBC驱动实现

    • system 本地jar,如短信jar

  3. exclusions

    • 排除,如果某个模块依赖的包和其他可能有冲突,可以通过exclusion将其排除,如springframework下的commons-logging

      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>${org.springframework-version}</version>
          <exclusions>
              <!-- Exclude Commons Logging in favor of SLF4f -->
              <exclusion>
                  <groupId>commons-logging</groupId>
                  <artifactId>commons-logging</artifactId>
              </exclusion>
          </exclusions>
      </dependency>
      
      

所有子模块版本升级

  1. 在父pom中加入插件

    <build>
     <plugins>
         <plugin>
             <groupId>org.codehaus.mojo</groupId>
             <artifactId>versions-maven-plugin</artifactId>
             <version>2.3</version>
         </plugin>
     </plugins>
    </build>
    
  2. 执行命令,将所有子模块版本指定为1.1-SNAPSHOT

    mvn versions:set -DnewVersion=1.1-SNAPSHOT
    

依赖仲裁

  • 最短路径原则
  • 加载先后原则

查看依赖树

mvn dependency:tree > d.txt

将依赖树打印并输出到d.txt中

生命周期

  1. lifecyle: 3个生命周期,clean、default、site,每个生命周期包含多个phase。每个周期中运行一个命令时,在这个周期里的其他在该命令之前的phase步骤都会执行。如:执行clean会自动执行pre-clean,但执行install并不会自动执行clean。

    maven生命周期.png
    > A Build  Lifecycle is made up of phases. 一个生命周期包含多个阶段
    
    > A Build Phase is made up of plugin goals. 
    
  2. phase: 指定生命周期中的哪一阶段

  3. goal:插件目标,

    <executions>
     <execution>
         <phase>package</phase>
            <goals>
                <goal></goal>
            </goals>
     </execution>
    </executions>
    
    

版本管理

  1. 什么是1.0-SNAPSHOT

    不稳定版本,开发中若别人更新了jar,mvn不会自动实时更新,此时可以先将其从本地repository删除

    再执行mvn clean package -U 强制从拉取jar

  2. 主版本号;次版本号;增量版本号-<里程碑版本>

    如:1.0.0-RELEASE

常用命令

  1. compile

  2. clean

    运行 mvn clean 时,会调用maven自带的clean插件,将项目下的target/ 删除,测试或发布时,先clean,否则可能用的修改前的target

  3. test test case junit/testNG

    mvn clean test

  4. package 打包

    mvn clean package

  5. install 把项目安装到本地仓库local repo

  6. **deploy ** 把本地jar发布到私服上

自定义插件

  1. pom中指定packing为 maven-plugin

    <groupId>cn.zys</groupId>
    <artifactId>my-plugin</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>maven-plugin</packaging>
    
  2. 加入依赖

    <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>
    
  3. 创建类继承AbstractMojo,这里name 定义的"test-plugin" 就是个 goal

    @Mojo(name="test-plugin",defaultPhase = LifecyclePhase.PACKAGE)
    public class MyMojo extends AbstractMojo {
        @Parameter
        private String param;
    
        @Parameter
        private List<String> options;
    
        public void execute() throws MojoExecutionException, MojoFailureException {
            System.out.println("Hello plugin" + param);
            System.out.println("Hello plugin" + options);
        }
    }
    
  4. mvn install

  5. 参数传递

    <plugins>
          <plugin>
            <groupId>cn.zys</groupId>
            <artifactId>my-plugin</artifactId>
            <configuration>
              <param>124</param>
              <options>
                <option>hello</option>
                <option>world</option>
              </options>
            </configuration>
            <executions>
              <execution>
                <phase>package</phase>
                <goals>
                  <goal>test-plugin</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
     </plugins>
    

profile

  • 使用场景:分环境打包dev/test/pro

Nexus私服

安装与运行

  1. 下载 这里我下载的版本是3.14.0

    https://sonatype-download.global.ssl.fastly.net/repository/repositoryManager/3/nexus-3.14.0-04-win64.zip

  2. 解压 安装

    压缩包解压后有两个文件夹 nexus-3.14.0-04sonatype-work

  3. 配置 nexus-3.14.0-04\etc 路径下,打开 nexus-default.properties 作如下配置:

    ## DO NOT EDIT - CUSTOMIZATIONS BELONG IN $data-dir/etc/nexus.properties
    ##
    # Jetty section
    application-port=8081
    application-host=127.0.0.1
    nexus-args=${jetty.etc}/jetty.xml,${jetty.etc}/jetty-http.xml,${jetty.etc}/jetty-requestlog.xml
    nexus-context-path=/
    
    # Nexus section
    nexus-edition=nexus-pro-edition
    nexus-features=\
     nexus-pro-feature
    
    
  4. 使用

    http://127.0.0.1:8081/nexus

    用户名:admin

    密码:admin123

jar包发布到私服

  1. 项目.pom中配置仓库id和地址,其中,id和name可以自己随便定义

    <distributionManagement>
        <repository>
          <id>nexus-releases</id>
          <name>Nexus Releases Repository</name>
          <url>http://localhost:8081/repository/maven-releases/</url>
        </repository>
        <snapshotRepository>
          <id>nexus-snapshots</id>
          <name>Nexus snapshots Repository</name>
          <url>http://localhost:8081/repository/maven-snapshots/</url>
        </snapshotRepository>
      </distributionManagement>
    
  2. maven 的setting.xml中配置服务器认证信息

      </servers>
     <server>
          <id>nexus-releases</id>
          <username>admin</username>
          <password>admin123</password>
        </server>
     <server>
          <id>nexus-snapshots</id>
          <username>admin</username>
          <password>admin123</password>
        </server>
      </servers>
    

    注意的是,这里的id要和pom文件中的id一致

从私服下载

  1. 可以配置mirror

    <mirror>  
     <id>nexus</id>  
     <name>Nexus Mirror</name>  
     <url>http://localhost:8081/repository/maven-public/</url>  
     <mirrorOf>nexus,central</mirrorOf>          
    </mirror> 
    
  2. 除配置mirror外,还可以配置profile来完成

    <profiles>
     <profile>
         <id>dev</id>
         
         <repositories>
            <repository>
              <id>local-nexus</id>
              <name>local nexus</name>
              <url>http://localhost:8081/repository/maven-public/</url>
           <releases>
             <enabled>true</enabled>
             <!-- 指定下载更新的频率。这里的选项是:always(一直),daily(每日,默认值),interval:X(这里X指分钟),或者never(从不) -->
                <!-- <updatePolicy>daily</updatePolicy> -->
           </releases>
              <snapshots>
             <enabled>true</enabled>
           </snapshots>
            </repository>
          </repositories>
       <pluginRepositories>
         <pluginRepository>
             <id>plugin-local-mirror</id>
             <url>http://localhost:8081/repository/maven-public/</url>
         </pluginRepository>
       </pluginRepositories>   
     </profile>
      </profiles>
      
      <activeProfiles>
        <activeProfile>dev</activeProfile>
      </activeProfiles>
    
    

archetype

  1. 生成一个模板

    mvn archetype:create-from-project
    
  2. cd 到项目的/target/generated-sources/archetype下

  3. mvn install

  4. 从archetype创建项目

    mvn archetype:generate -DarchetypeCatalog=local
    

相关文章

网友评论

      本文标题:maven

      本文链接:https://www.haomeiwen.com/subject/qwkdlqtx.html