Maven

1、maven安装

  1》http://maven.apache.org/download.cgi下载apache-maven-3.6.1

  2》解压缩安装包到指定的文件夹,如C:fyliusoftwareapache-maven-3.6.1

  3》配置maven用户环境变量MAVEN_HOME

  4》设置Http访问代理

  5》配置本地库路径

  6》配置镜像

<mirror>
      <id>alimaven</id>
      <mirrorOf>central</mirrorOf>
      <name>Human Readable Name for this Mirror.</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>

  7》配置用户范围setting.xml

2、maven的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
         http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    
    <groupId>com.lfy.cn</groupId>
    <artifactId>helloworld</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>maven helloworld project</name>
</project>
说明:
project元素:是所有pom.xml的根元素。
modelVersion:指定当前pom模型的版本,maven2、maven3只能是4.0.0
groupId:定义项目属于哪个组
artifactId:定义当前maven项目在组中的唯一ID
version:指定了项目当前的版本
groupId、artifactId、version:定义了一个项目的基本坐标。
#clean告诉maven清理输出目录target/
#compile告诉maven编译项目主代码
mvn clean compile
<!-- scope:表示依赖范围,默认为compile,表示该依赖对主代码、测试代码都有效。
test表示该依赖只对测试有效,即测试代码中import junit没问题,但是如果
我们在主代码中import junit,就会造成编译错误。-->
<
dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies>

3、打包和运行

#默认打包类型为jar
#package会执行编译、测试、打包的过程
mvn clean package

4、打包和安装

#install将会执行编译、测试、打包、安装到本地库
mvn clean install

5、为了使maven打包的jar能够有main方法的入口信息,在pom.xml中添加如下插件

    <build>  
        <plugins>  
            <plugin>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-shade-plugin</artifactId>  
                <version>2.4.1</version>  
                <executions>  
                    <execution>  
                        <phase>package</phase>  
                        <goals>  
                            <goal>shade</goal>  
                        </goals>  
                        <configuration>  
                            <transformers>  
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">  
                                    <mainClass>com.lfy.mvnbook.helloworld.HelloWorld</mainClass>  
                                </transformer>  
                            </transformers>  
                        </configuration>  
                    </execution>  
                </executions>  
            </plugin>    
        </plugins>  
    </build>

6、使用Archetype生成项目骨架

  我们也可以开发自己的Archetype项目以生成自己的项目骨架。选择不同的Archetype项目模板,可以生成快速启动项目、Webapp项目。

C:fyliulfyTempmvnProjectmvn-archetype-generate>mvn archetype:generate
[INFO] Scanning for projects...
Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/o
rg/apache/maven/plugins/maven-archetype-plugin/maven-metadata.xml
...
Downloaded from alimaven: http://maven.aliyun.com/nexus/content/groups/public/or
g/apache/maven/plugins/maven-archetype-plugin/3.1.2/maven-archetype-plugin-3.1.2
.jar (97 kB at 82 kB/s)
[INFO]
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:3.1.2:generate (default-cli) > generate-source
s @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:3.1.2:generate (default-cli) < generate-source
s @ standalone-pom <<<
[INFO]
[INFO]
[INFO] --- maven-archetype-plugin:3.1.2:generate (default-cli) @ standalone-pom
---
Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/o
rg/apache/maven/archetype/archetype-catalog/3.1.2/archetype-catalog-3.1.2.pom
...
Downloaded from alimaven: http://maven.aliyun.com/nexus/content/groups/public/or
g/apache/ant/ant/1.8.1/ant-1.8.1.jar (1.5 MB at 128 kB/s)
[INFO] Generating project in Interactive mode
[WARNING] No archetype found in remote catalog. Defaulting to internal catalog
[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.
archetypes:maven-archetype-quickstart:1.0)
Choose archetype:
1: internal -> org.apache.maven.archetypes:maven-archetype-archetype (An archety
pe which contains a sample archetype.)
2: internal -> org.apache.maven.archetypes:maven-archetype-j2ee-simple (An arche
type which contains a simplifed sample J2EE application.)
3: internal -> org.apache.maven.archetypes:maven-archetype-plugin (An archetype
which contains a sample Maven plugin.)
4: internal -> org.apache.maven.archetypes:maven-archetype-plugin-site (An arche
type which contains a sample Maven plugin site.
      This archetype can be layered upon an existing Maven plugin project.)
5: internal -> org.apache.maven.archetypes:maven-archetype-portlet (An archetype
 which contains a sample JSR-268 Portlet.)
6: internal -> org.apache.maven.archetypes:maven-archetype-profiles ()
7: internal -> org.apache.maven.archetypes:maven-archetype-quickstart (An archet
ype which contains a sample Maven project.)
8: internal -> org.apache.maven.archetypes:maven-archetype-site (An archetype wh
ich contains a sample Maven site which demonstrates
      some of the supported document types like APT, XDoc, and FML and demonstra
tes how
      to i18n your site. This archetype can be layered upon an existing Maven pr
oject.)
9: internal -> org.apache.maven.archetypes:maven-archetype-site-simple (An arche
type which contains a sample Maven site.)
10: internal -> org.apache.maven.archetypes:maven-archetype-webapp (An archetype
 which contains a sample Maven Webapp project.)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive co
ntains): 7:
Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/o
rg/apache/maven/archetypes/maven-archetype-bundles/4/maven-archetype-bundles-4.p
om
...
Downloaded from alimaven: http://maven.aliyun.com/nexus/content/groups/public/or
g/apache/maven/archetype/maven-archetype/2.0-alpha-5/maven-archetype-2.0-alpha-5
.pom (8.7 kB at 11 kB/s)
Define value for property 'groupId': com.lfy.mvnbook
Define value for property 'artifactId': hello-world
Define value for property 'version' 1.0-SNAPSHOT: :
Define value for property 'package' com.lfy.mvnbook: : com.lfy.mvnbook.helloworl
d
Confirm properties configuration:
groupId: com.lfy.mvnbook
artifactId: hello-world
version: 1.0-SNAPSHOT
package: com.lfy.mvnbook.helloworld
 Y: : Y
[INFO] -------------------------------------------------------------------------
---
[INFO] Using following parameters for creating project from Old (1.x) Archetype:
 maven-archetype-quickstart:1.1
[INFO] -------------------------------------------------------------------------
---
[INFO] Parameter: basedir, Value: C:fyliulfyTempmvnProjectmvn-archetype-gene
rate
[INFO] Parameter: package, Value: com.lfy.mvnbook.helloworld
[INFO] Parameter: groupId, Value: com.lfy.mvnbook
[INFO] Parameter: artifactId, Value: hello-world
[INFO] Parameter: packageName, Value: com.lfy.mvnbook.helloworld
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: C:fyliulfyTempmvnProj
ectmvn-archetype-generatehello-world
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  04:52 min
[INFO] Finished at: 2019-09-15T00:49:09+08:00
[INFO] ------------------------------------------------------------------------

C:fyliulfyTempmvnProjectmvn-archetype-generate>

7、使用Eclipse就可以导入我们上面使用骨架语句创建的项目了

8、 可以使用Eclipse创建maven快速启动项目、Webapp项目

9、Eclipse中运行mvn命令

   可以在pom.xml上右键,Run as。如果列表中的命令不能一次满足我们的mvn执行需求,可以选择Maven build...自定义maven命令,如在Goals中输入clean test。

原文地址:https://www.cnblogs.com/ZeroMZ/p/11519042.html