【MAVEN】常用配置记录

1. 配置文件修改

  - 修改local库的位置

  - 配置源mirror,推荐很流畅的一个阿里源2017.3.30流畅可用

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
    
    <localRepository>D:Program Filesmvn_repo</localRepository>
    
    <mirrors>
        <!-- 阿里云仓库 -->
        <mirror>
            <id>alimaven</id>
            <mirrorOf>central</mirrorOf>
            <name>aliyun maven</name>
            <url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
        </mirror>
    
        <!-- 中央仓库1 -->
        <mirror>
            <id>repo1</id>
            <mirrorOf>central</mirrorOf>
            <name>Human Readable Name for this Mirror.</name>
            <url>http://repo1.maven.org/maven2/</url>
        </mirror>
    
        <!-- 中央仓库2 -->
        <mirror>
            <id>repo2</id>
            <mirrorOf>central</mirrorOf>
            <name>Human Readable Name for this Mirror.</name>
            <url>http://repo2.maven.org/maven2/</url>
        </mirror>
    </mirrors> 
</settings>

2. maven的几个基础概念

groupId: 项目的组织机构,也是java包的目录结构,一般都是域名的倒序,比如 com.firefoxbug.www
artifactId: 项目实际的名字,比如log4j
packaging: 包的类型,比如jar
version: 项目版本号,比如1.0-SNAPSHOT

创建工程

$ groupId=com.firefoxbug.www
$ artifactId=HelloWorld
$ version=1.0-SNAPSHOT
$ mvn archetype:generate -DgroupId=${groupId} -DartifactId=${artifactId} -Dpackage=${groupId} -Dversion=${version}

形成的目录

HelloWorld/
├── pom.xml
└── src
    ├── main
    │   └── java
    │       └── com
    │           └── firefoxbug
    │               └── www
    │                   └── App.java
    └── test
        └── java
            └── com
                └── firefoxbug
                    └── www
                        └── AppTest.java

3. pom.xml

标签说明

<project>       pom文件的顶级节点
<modelVersion>     object model版本,对Maven2和Maven3来说,只能是4.0.0 
<groupId>       项目创建组织的标识符,一般是域名的倒写
<artifactId>      定义了项目在所属组织的标识符下的唯一标识,一个组织下可以有多个项目
<version>        当前项目的版本,SNAPSHOT,表示是快照版本,在开发中

<packaging>      打包的方式,有jar、war、ear等
<name>        项目的名称
<url>          项目的地址

<properties>    属性配置,比如:<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<dependencies>     构建项目依赖的jar
<project >
  <groupId>com.firefoxbug.helloworld</groupId>
  <artifactId>helloworld</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>helloworld</name>

  <dependencies>
    <dependency>
    </dependency>
  </dependencies>
</project>

如果添加httpclient,http://mvnrepository.com/中搜索httpclient

以4.5.2为例,复制dependency标签插入dependencies标签内,

<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpclient</artifactId>
  <version>4.5.2</version>
</dependency>

4. maven 常用 cli

1. mvn compile 编译源代码
2. mvn test-compile 编译测试代码
3. mvn test 运行测试
5. mvn package 打包,根据pom.xml打成war或jar
    如果pom.xml中设置 war,则此命令相当于mvn war:war
    如果pom.xml中设置 jar,则此命令相当于mvn jar:jar
6. mvn -Dtest package   
    打包但不编译test例子:mvn package -Dmaven.test.skip=true
    打包、编译但是不运行test例子: mvn package -DskipTests
7. mvn install  在本地Repository中安装jar,就把maven构建项目的【清理】→【编译】→【测试】→【打包】的这几个过程都做了,同时将打包好的jar包发布到本地的Maven仓库中
8. mvn clean    清除产生的项目
9. mvn eclipse:eclipse 生成eclipse项目
10.mvn idea:idea    生成idea项目
11.mvn eclipse:clean    清除eclipse的一些系统设置

支持组合命令

maven clean compile

查看dependency:tree

mvn dependency:tree

执行测试代码

mvn clean test

CLI创建maven项目,使用"mvn archetype:generate"命令和"mvn archetype:create"都可以创建项目,目前没有发现这两者的区别,

唯一区别的地方就是发现使用"mvn archetype:generate"命令创建项目时要特别长的时间才能够将项目创建好,而使用"mvn archetype:create"命令则可以很快将项目创建出来。

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=myapp -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

或者

mvn archetype:create -DgroupId=com.mycompany.app -DartifactId=myapp -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

BUILD SUCCESS就表示项目构建成功

5. 打包的几种方式

  maven编译打包可以支持三种模式,分别如下:

    -no-dependencies: 这种模式下target下面不会出现任何依赖的包,必须手动指定-classpath设置所在环境的依赖包路径;

    -with-dependencies: 这种模式会把所有外部依赖的包都打进一个jar包,只需要把jar包发布到线上即可运行;

    -copy-dependencies: 这种模式会把外部依赖的jar包都独立生成到target/下面,运行时候手动指定-classpath;

  -with-dependencies例子,使用插件maven-assembly-plugin

<project>
    ...
    <build>
        <sourceDirectory>src/main/java</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>attached</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    ...
</project>

-copy-dependencies: 使用插件org.apache.maven.plugins

<project>
    ...
    <build>
        <sourceDirectory>src/main/java</sourceDirectory>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>
                                ${project.build.directory}
                            </outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    ...
</project>

6. 创建webL类型的项目

mvn archetype:generate

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-WebApp -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

 打包成war,送至tomcat发布

 7. 常用插件 org.apache.maven.plugins

maven-compiler-plugin等

举例

<build>
        <finalName>HttpClientUtil</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <appendAssemblyId>false</appendAssemblyId>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>JmeterJavaReq.HttpClientUtil</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>assembly</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

8. 以来范围 scope

转载http://www.cnblogs.com/mxm2005/p/4947905.html 

Maven的scope依赖作用域说明

1test范围指的是测试范围有效,在编译和打包时都不会使用这个依赖

2compile范围指的是编译范围有效,在编译和打包时都会将依赖存储进去

3provided依赖:在编译和测试的过程有效,最后生成war包时不会加入,诸如:servlet-api,因为servlet-apitomcatweb服务器已经存在了,如果再打包会冲突 

4runtime在运行的时候依赖,在编译的时候不依赖 

默认的依赖范围是compile

<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>

依赖的传递:

作用域是test的包不会传递到引用这个项目的其它项目,但如果不是test会传递依赖到其它项目。

如:项目A中有一个依赖包junit4.10,它的作用域是test

现在有一个项目B,引用项目A,如果项目B要使用junit4.10就必须自己重新定义依赖关系。【因为不会传递依赖,所以不会从项目A中得到】

但:如果作用域是其它的,不是test

那么项目B可以直接使用不用自己再定义一个依赖关系。【因为会从项目A中自动传递依赖,而得到】

依赖关系的传递(间接依赖)

a) 同级别依赖
-->(依赖)

项目A-->P包1.0
项目B-->P包2.0

项目C-->项目A,项目B

项目C得到的P包,应该是哪个呢?
应该是项目A的P包1.0,因为在项目C中,项目A依赖关系定义在前,项目B的依赖配置定义在后。

b) 不同级别依赖,层级少的优先依赖
如:
A-->P1.0
B-->A
C-->B

D-->P2.0

E-->C,D 那么,E通过传递依赖得到的将会是:P2.0 (就算E中的C依赖定义在前面)

c) 排除依赖,当包通过传递,产生了冲突时可能通过排队排除关系解决
当一个项目A依赖项目B,而项目B同时依赖项目C,如果项目A中因为各种原因不想引用项目C,在配置项目B的依赖时,可以排除对C的依赖

示例(假设配置的是A的pom.xml,依赖关系为:A –> B; B –> C):

<project>
  ...
  <dependencies>
    <dependency>
      <groupId>sample.ProjectB</groupId>
      <artifactId>Project-B</artifactId>
      <version>1.0</version>
      <scope>compile</scope>
      <exclusions>
        <exclusion>  <!-- declare the exclusion here -->
          <groupId>sample.ProjectC</groupId>
          <artifactId>Project-C</artifactId>
        </exclusion>
      </exclusions> 
    </dependency>
  </dependencies>
</project>

 

参考的文章

http://www.tuicool.com/articles/jMRVb2
http://www.cnblogs.com/xdp-gacl/tag/Maven%E5%AD%A6%E4%B9%A0%E6%80%BB%E7%BB%93/
原文地址:https://www.cnblogs.com/AlexBai326/p/6648957.html