码迷,mamicode.com
首页 > 其他好文 > 详细

【MAVEN】常用配置记录

时间:2017-03-31 01:11:07      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:路径   命令   files   版本号   tom   idea   version   jar包   mirrors   

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 Files\mvn_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>

 

参考的文章

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/

 

【MAVEN】常用配置记录

标签:路径   命令   files   版本号   tom   idea   version   jar包   mirrors   

原文地址:http://www.cnblogs.com/AlexBai326/p/6648957.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!