maven学习笔记(二)

仓库(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>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,406评论 6 503
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,732评论 3 393
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,711评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,380评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,432评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,301评论 1 301
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,145评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,008评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,443评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,649评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,795评论 1 347
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,501评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,119评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,731评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,865评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,899评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,724评论 2 354

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,654评论 18 139
  • 所有项目的构建都是有生命周期的,这个生命周期包括:项目清理、初始化、编译、测试、打包、集成测试、验证、部署、站点生...
    zlcook阅读 2,769评论 0 21
  • 文章摘要1、Maven 第一个工程,HelloWorld创建详情2、POM元素、archetype自动创建工程。3...
    Android那些事儿阅读 639评论 0 51
  • 简介 概述 Maven 是一个项目管理和整合工具 Maven 为开发者提供了一套完整的构建生命周期框架 Maven...
    闽越布衣阅读 4,293评论 6 39
  • 2012年,我说如果等到世界末日,这个世界没有毁灭,我们就在一起。然而,我用命许下的诺言,终究没有敌的过现实。 轰...
    城南说阅读 214评论 0 2