mvn使用

mvn 使用(clean,package,install)

最近在看netty的源码,用maven构建,出现了各种问题,记录下使用到的mvn的命令和一些基本使用。

  1. mvn修改默认java版本:
    ${MAVEN_HOME}\conf\settings.xml
	 <profile>
      <id>jdk18</id>

      <activation>
		<activeByDefault>true</activeByDefault>
        <jdk>1.8</jdk>
      </activation>
	  <properties>
		<maven.compile.source>1.8</maven.compile.source>
		<maven.compile.target>1.8</maven.compile.target>
		<maven.compile.complieVersion>1.8</maven.compile.complieVersion>
	  </properties>
	</profile>
  1. 修改局部配置
  <build>
  	<plugins>
  		<plugin>
  			<groupId>org.apache.maven.plugins</groupId>
  			<artifactId>maven-compiler-plugin</artifactId>
  			<configuration>
  				<source>1.8</source>
  				<target>1.8</target>
  			</configuration>
  		</plugin>
  	</plugins>
  </build>
  • maven 生命周期 && 插件
nameprocessplugin
cleanpre-clean -> clean -> post-cleanmaven-clean-plugin
default…-> compile ->…test-compile -> test -> … -> package ->… install -> deploymaven-resource-plugin,maven-compiler-plugin,maven-surefire-plugin,maven-jar-plugin,maven-install-plugin,maven-deploy-plugin
sitepre-site -> site -> post-site -> site-deploymaven-site-plugin

get the plugin from: http://maven.apache.org/plugins/index.html
http://repository.codehaus.org/org/codehaus/mojo/
use the : mvn help: describe -Dplugin = org.apache.,maven.plugins: maven-complier-plugin:2.1

  • maven 依赖
    传递性依赖:
    调节依赖:路径最近者优先,顺序最靠前优先
    可选依赖:option 设成true,不被传递
     <dependency>
      <groupId>javax.mail</groupId>
      <artifactId>mail</artifactId>
      <version>1.4.1</version>
      <optional>true</optional>
    </dependency>
 排除依赖:exclusion.
    <dependency>
      <groupId>com.icegreen</groupId>
      <artifactId>greenmail</artifactId>
      <version>1.3.1b</version>
      <exclusions>
      	<groupId>some</groupId>
      	<artifactId>some</artifactId>
      </exclusions>
      <scope>test</scope>
    </dependency>

归并依赖:

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <springframe.version>2.5.6</springframe.version>
  </properties>
  ......
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${springframe.version}</version>
    </dependency>

查看依赖解析:mvn dependency:list , mvn denpendency:tree
依赖分析: mvn dependenvy:analyze(used undeclared dependencies , unused declared dependencies)

  • maven 为什么reource 文件夹中的内容会自动添加到classes目录?

maven 约定优于配置

  • 源码目录:src/main/java
  • 编译输出目录:target/classes
  • 打包方式:jar
  • 包输出目录:target/
    超级POM中的配置
    apache-maven-3.5.3\lib\maven-model-builder-3.5.3\org\apache\maven\model\pom-4.0.0.xml\
<project>
  <modelVersion>4.0.0</modelVersion>

  <repositories>
    <repository>
      <id>central</id>
      <name>Central Repository</name>
      <url>https://repo.maven.apache.org/maven2</url>
      <layout>default</layout>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
  </repositories>

  <pluginRepositories>
    <pluginRepository>
      <id>central</id>
      <name>Central Repository</name>
      <url>https://repo.maven.apache.org/maven2</url>
      <layout>default</layout>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
    </pluginRepository>
  </pluginRepositories>

  <build>
    <directory>${project.basedir}/target</directory>
    <outputDirectory>${project.build.directory}/classes</outputDirectory>
    <finalName>${project.artifactId}-${project.version}</finalName>
    <testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
    <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
    <scriptSourceDirectory>${project.basedir}/src/main/scripts</scriptSourceDirectory>
    <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
    <resources>
      <resource>
        <directory>${project.basedir}/src/main/resources</directory>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <directory>${project.basedir}/src/test/resources</directory>
      </testResource>
    </testResources>
    <pluginManagement>
      <!-- NOTE: These plugins will be removed from future versions of the super POM -->
      <!-- They are kept for the moment as they are very unlikely to conflict with lifecycle mappings (MNG-4453) -->
      <plugins>
        <plugin>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>1.3</version>
        </plugin>
        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>2.2-beta-5</version>
        </plugin>
        <plugin>
          <artifactId>maven-dependency-plugin</artifactId>
          <version>2.8</version>
        </plugin>
        <plugin>
          <artifactId>maven-release-plugin</artifactId>
          <version>2.5.3</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

  <reporting>
    <outputDirectory>${project.build.directory}/site</outputDirectory>
  </reporting>

  <profiles>
    <!-- NOTE: The release profile will be removed from future versions of the super POM -->
    <profile>
      <id>release-profile</id>

      <activation>
        <property>
          <name>performRelease</name>
          <value>true</value>
        </property>
      </activation>

      <build>
        <plugins>
          <plugin>
            <inherited>true</inherited>
            <artifactId>maven-source-plugin</artifactId>
            <executions>
              <execution>
                <id>attach-sources</id>
                <goals>
                  <goal>jar</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <inherited>true</inherited>
            <artifactId>maven-javadoc-plugin</artifactId>
            <executions>
              <execution>
                <id>attach-javadocs</id>
                <goals>
                  <goal>jar</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <inherited>true</inherited>
            <artifactId>maven-deploy-plugin</artifactId>
            <configuration>
              <updateReleaseInfo>true</updateReleaseInfo>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>

</project>

mvn test 添加和排除运行测试类

  <build>
  	<plugins>
  		<plugin>
  			<groupId>org.apache.maven.plugins</groupId>
  			<artifactId>maven-surefire-plugin</artifactId>
  			<version>2.5</version>
  			<configuration>
  				<includes>
  					<include>**/*Tests.java</include>
  				</includes>
  				<excludes>
  					<exclude>**/*ServiceTests.java</exclude>
  				</excludes>
  			</configuration>
  		</plugin>
  	</plugins>
  </build>

打包测试类

  		<plugin>
  			<groupId>org.apache.maven.plugins</groupId>
  			<artifactId>maven-ja-plugin</artifactId>
  			<version>2.2</version>
  			<executions>
  				<execution>
  					<goals>
  						<goal>test-jar</goal>
  					</goals>
  				</execution>
  			</executions>
  		</plugin>

一些基本的命令
mvn clean package -Dmaven.test.skip=true
mvn clean package -DskipTests
mvn test -Dtest=RandomTest (类名)
mvn test -Dtest=Random*Test (类名)
mvn test -Dtest=RandomTest1,RandomTest2
mvn test -Dtest -DfailIfNoTests=false(没有测试类不报错)
mvn cobertura:cobetura

以后用到了还会再加

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值