美文网首页
maven学习笔记(二)

maven学习笔记(二)

作者: 味道_3a01 | 来源:发表于2018-09-17 21:51 被阅读5次

    仓库(nexus)

    A-->core
    B-->service
    C-->action
    本地仓库、私有仓库、中央仓库
    私服 nexus sonatype
    nexus command:start stop restart install uninstall
    默认端口号 8081
    localhost:8081/nexus
    默认用户名和密码 admin/admin123

    安装nexus的前提是,服务器上需要安装maven,否则,就算安装了nexus,也无法访问

    mvn:deploy 打包到仓库
    配置私有仓库:

     <profile>
        <id>nexusProfile</id>
        <repositories>
            <repository>
             <id>nexus</id>
                  <name>nexus repository</name>
                <url>http://192.168.190.128:8081/repository/maven-public/</url>
                  <releases>
                <enabled>true</enabled>
                  </releases>
                  <!-- snapshots默认是关闭的,需要手动开启 -->
                  <snapshots>
                <enabled>true</enabled>
                  </snapshots>
            </repository>
          </repositories>
        </profile>
        
        ...
        
        <activeProfiles>
        <!-- 只有激活才生效 -->
        <activeProfile>nexusProfile</activeProfile>
      </activeProfiles>
    

    当本地仓库无法提供服务时,仍然会去中央仓库下载;
    如果不想让去中央仓库下载,可以配置镜像

    <!-- 工厂的镜像,只要mirrorOf中的工厂要访问,都会自动来找镜像,如果镜像无法访问就不会再去中央工厂,使用*表示所有的工厂都使用这个镜像访问,这是推荐的做法 -->
    <mirror>
        <id>central</id>
       <!-- <mirrorOf>nexus,central</mirrorOf> -->
        <mirrorOf>*</mirrorOf>
        <name>Maven Repository Switchboard</name>
        <url>http://192.168.190.128:8081/repository/maven-public/</url>
    </mirror>
    

    索引文件

    发布项目到nexus中

    pom文件

    <distributionManagement>
        <repository>
          <id>user-release</id>
          <name>user release resp</name>
          <url>http://192.168.190.128:8081/repository/maven-releases/</url>
        </repository>
        <snapshotRepository>
          <id>user-snapshots</id>
          <name>user snapshots resp</name>
          <url>http://192.168.190.128:8081/repository/maven-snapshots/</url>
        </snapshotRepository>
      </distributionManagement>
    

    settings.xml

       <server>
          <id>user-release</id><!--对应pom文件中repository的id-->
          <username>deployment</username>
          <password>deployment123</password>
        </server>
          <server>
          <id>user-snapshots</id>
          <username>deployment</username>
          <password>deployment123</password>
        </server>
    

    nexus管理

    创建仓库、创建角色、创建用户

    生命周期和插件

    三套生命周期

    • clean
      • pre-clean 执行一些需要在clean之前完成的工作
      • clean 移出所有上一次构建生成的文件
      • post-clean 执行一些需要在clean之后立刻完成的工作
    • compile
      • validate
      • generate-sources
      • process-sources
      • generate-resources
      • process-resources 复制并处理资源文件,至目标文件,准备打包
      • compile 编译项目的源代码
      • process-classes
      • generate-test-sources
      • process-test-sources
      • generate-test-resources
      • process-test-resources 复制并处理资源文件,至目标测试目录
      • test-compile 编译测试源代码
      • process-test-classes
      • test 使用合适的单元测试框架运行测试。这些测试代码不会被打包部署
      • prepare-package
      • package 打包成jar或者war或者其他格式的分发包
      • pre-integration-test
      • integration-test
      • post-integration-test
      • verify
      • install 将打好的包安装到本地仓库,供其他项目使用
      • deploy 将打好的包安装到远程仓库,供其他项目使用
    • site 生成站点
      • pre-site
      • site 生成项目的站点文件
      • post-site
      • site-deploy 发布生成的站点文档
        image
        compile 插件源码

    如果希望将源文件打包的话,需要使用source插件

     <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <version>3.0.1</version>
            <executions>
              <execution>
                <!--生命周期-->
                <phase>compile</phase>
                <goals><!--目标-->
                  <goal>jar</goal>
                  <goal>test-jar</goal>
                </goals>
              </execution>
            </executions>
            <configuration>
              <outputDirectory>/absolute/path/to/the/output/directory</outputDirectory>
              <finalName>filename-of-generated-jar-file</finalName>
              <attach>false</attach>
            </configuration>
          </plugin>
        </plugins>
      </build>
    

    插件的基础

    指定编译的jdk版本

     <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
              <verbose>true</verbose>
              <fork>true</fork>
              <executable>
                <!-- path-to-javac -->
                ${JAVA_1_4_HOME}/bin/javac
              </executable>
              <compilerVersion>1.8</compilerVersion>
            </configuration>
          </plugin>
    

    plugin 也可以继承

    <build>
     <pluginManagement>
          <plugins>
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-compiler-plugin</artifactId>
              <version>3.8.0</version>
              <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <!--此处设置了skip等于设置了把所有的测试编译都跳过,如果测试类写得有问题,也不会报错,所以一般不使用-->
                <skip>true</skip>
              </configuration>
            </plugin>
          </plugins>
        </pluginManagement>
      </build>
    

    插件的应用

    rar插件

        <plugins>
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-rar-plugin</artifactId>
              <version>2.4</version>
              <executions>
                <execution>
                  <phase>package</phase>
                  <goals><goal>rar</goal></goals>
                </execution>
              </executions>
              <configuration>
                <includeJar>true</includeJar>
              </configuration>
            </plugin>
          </plugins>
    

    sql插件

          <plugins>
           <plugin>
              <groupId>org.codehaus.mojo</groupId>
              <artifactId>sql-maven-plugin</artifactId>
              <version>3.0.0-SNAPSHOT</version>
              <!-- 使用插件依然可以指定相应的依赖 -->
              <dependencies>
                  <dependency>
                      <groupId>mysql</groupId>
                      <artifactId>mysql-connector-java</artifactId>
                    <version>5.1.24</version>
                  </dependency>
              </dependencies>
              <configuration>
                <driver></driver>
                <url></url>
                <username></username>
                <password></password>
              </configuration>
              <executions>
                <execution>
                  <id>create-db</id>
                  <phase>compile</phase>
                  <goals><goal>execute</goal></goals>
                  <configuration>
                    <sqlCommand>create database IF NOT EXISTS itat_maven_test</sqlCommand>
                  </configuration>
                </execution>
                <execution>
                  <id>init-table</id>
                  <phase>test-compile</phase>
                  <goals><goal>execute</goal></goals>
                  <configuration>
                    <srcFiles>
                      <srcFile>src/main/resources/init.sql</srcFile>
                    </srcFiles>
                  </configuration>
                </execution>
              </executions>
            </plugin>
          </plugins>
    

    插件也有groupId、artifactId,多了目标 生命周期

    测试

    测试相关的类和资源放在test包下
    执行mvn test时,默认只会执行如下三类测试用例

    • Test**
    • **Test
    • **TestCase

    如果不是以上三种命名规则,可以通过surefire插件来配置需要执行的测试类

    <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-surefire-plugin</artifactId>
              <version>2.22.0</version>
              <configuration>
                <!-- 设置包含的测试类 -->
                <includes>
                  <include>**/Hello*</include>
                </includes>
                <!-- 设置不进行测试的类 -->
                <!--  <excludes>
                  <exclude>Test*</exclude>
                </excludes>-->
                <!-- 跳过测试阶段,一般不推荐跳过 -->
                <skip>true</skip>
              </configuration>
            </plugin>
    

    在没有网络的情况下,可以通过help插件来查找帮助信息

    example

    mvn help:describe -DgroupId=org.apache.maven.plugins -DartifactId=maven-compiler-plugin -Dversion=3.8.0

    [root@eth-01 ~]# mvn help:describe -DgroupId=org.apache.maven.plugins -DartifactId=maven-compiler-plugin -Dversion=3.8.0
    [INFO] Scanning for projects...
    Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-clean-plugin/2.4.1/maven-clean-plugin-2.4.1.pom
    Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-clean-plugin/2.4.1/maven-clean-plugin-2.4.1.pom (5 KB at 1.2 KB/sec)
    Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/18/maven-plugins-18.pom
    Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/18/maven-plugins-18.pom (13 KB at 19.3 KB/sec)
    Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-parent/16/maven-parent-16.pom
    Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-parent/16/maven-parent-16.pom (23 KB at 43.0 KB/sec)
    ...
    [INFO] org.apache.maven.plugins:maven-compiler-plugin:3.8.0
    
    Name: Apache Maven Compiler Plugin
    Description: The Compiler Plugin is used to compile the sources of your
      project.
    Group Id: org.apache.maven.plugins
    Artifact Id: maven-compiler-plugin
    Version: 3.8.0
    Goal Prefix: compiler
    
    This plugin has 3 goals:
    
    compiler:compile
      Description: Compiles application sources
    
    compiler:help
      Description: Display help information on maven-compiler-plugin.
        Call mvn compiler:help -Ddetail=true -Dgoal=<goal-name> to display
        parameter details.
    
    compiler:testCompile
      Description: Compiles application test sources.
    
    For more information, run 'mvn help:describe [...] -Ddetail'
    
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 2:32.142s
    [INFO] Finished at: Mon Sep 17 20:07:25 CST 2018
    [INFO] Final Memory: 12M/30M
    [INFO] ------------------------------------------------------------------------
    
    

    手动跳过测试

    mvn clean package -DskipTests=true

    指定测试类
    mvn clean package -Dtest=Hello.java

    生成测试覆盖率
    cobertura
    命令方式:mvn cobertura
    在target/site下会有cobertura
    插件方式

        <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>cobertura-maven-plugin</artifactId>
          <configuration>
            <formats>
              <format>html</format>
              <format>xml</format>
            </formats>
          </configuration>
          <executions>
            <execution>
              <id>cobertura-report</id>
              <goals><goal>cobertura</goal></goals>
              <phase>test</phase>
            </execution>
          </executions>
        </plugin>
    

    发布web项目

    1. 手动方式
      命令:mvn clean package
      会在target下生成XX.war,然后将xx.war复制到tomcat/webapp下,启动tomcat

    2. 打包后自动copy
      maven copy plugin

    <plugin>
      <groupId>com.github.goldin</groupId>
      <artifactId>copy-maven-plugin</artifactId>
      <version>0.2.5</version>
      <executions>
        <execution>
          <id>copy-war</id>
          <phase>package</phase>
          <goals>
            <goal>copy</goal>
          </goals>
          <configuration>
            <resources>
              <resource>
                <targetPath>${catalina.home}/webapp</targetPath>
                <directory>${project.build.directory}</directory>
                <includes>
                  <include>user-web.war</include>
                </includes>
              </resource>
            </resources>
          </configuration>
        </execution>
      </executions>
    </plugin>
    
    1. 自动扫描变化,并打包
      jetty插件
    <plugin>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <configuration>
         <!-- 每隔10秒钟会自动扫描-->
          <scanIntervalSeconds>10</scanIntervalSeconds>
          <webApp>
            <contextPath>/hello</contextPath>
          </webApp>
          <connectors>
            <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
              <port>8080</port>
              <maxIdleTime>60000</maxIdleTime>
            </connector>
          </connectors>
        </configuration>
      </plugin>
    
    关注公众号,获取海量免费java进阶视频

    相关文章

      网友评论

          本文标题:maven学习笔记(二)

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