本文主要是建立一个Maven项目,项目打包并运行。
文章信息来源于Maven官网 Maven in five minutes
http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html
首先先按照下面命令来创建一个Maven Project,可能会懵,命令什么意思啊,里面的参数命令又是干嘛的。这个后面介绍,现在先看看我们用Maven创建的项目到底长什么样,项目结构如何。
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app \
-DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
执行完命令后maven显示执行过程,成功会显示Build Success
[INFO] Scanning for projects…
[INFO]
[INFO] ————————————————————————
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ————————————————————————
[INFO]
[INFO] >>> maven-archetype-plugin:3.0.0:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:3.0.0:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO] — maven-archetype-plugin:3.0.0:generate (default-cli) @ standalone-pom —
[INFO] Generating project in Batch mode
[WARNING] No archetype found in remote catalog. Defaulting to internal catalog
[INFO] —————————————————————————-
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.0
[INFO] —————————————————————————-
[INFO] Parameter: groupId, Value: com.mycompany.app
[INFO] Parameter: packageName, Value: com.mycompany.app
[INFO] Parameter: package, Value: com.mycompany.app
[INFO] Parameter: artifactId, Value: my-app
[INFO] Parameter: basedir, Value: /Users/cosmicbugs/it/ideaworks/myself
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: /Users/cosmicbugs/it/ideaworks/myself/my-app
[INFO] ————————————————————————
[INFO] BUILD SUCCESS
[INFO] ————————————————————————
[INFO] Total time: 3.136 s
[INFO] Finished at: 2017-03-19T22:27:38+08:00
[INFO] Final Memory: 16M/245M
[INFO] ————————————————————————
命令执行成功后,会发现当前目录多了一个my-app的目录,这个就是我们用Maven命令创建的项目。先看看项目里的目录结构到底长什么样
├── pom.xml
└── src
├── main
│ └── java
│ └── com
│ └── mycompany
│ └── app
│ └── App.java
└── test
└── java
└── com
└── mycompany
└── app
└── AppTest.java
项目目录结构中主要包含三部分:
pom.xml最为Maven最核心的一部分,具体内容如下
<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.0modelVersion>
<groupId>com.mycompany.appgroupId>
<artifactId>my-appartifactId>
<packaging>jarpackaging>
<version>1.0-SNAPSHOTversion>
<name>my-appname>
<url>http://maven.apache.orgurl>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>3.8.1version>
<scope>testscope>
dependency>
dependencies>
project>
pom.xml文件里具体作用,我们能使用哪些功能在后面介绍。本篇主要就是看看Maven项目到底长什么样。
Maven项目创建完毕之后就是想如何将代码转为我们需要的Jar或者War包。
在pom.xml文件当前路径执行下面命令,进行打包
mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building my-app 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ my-app ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/cosmicbugs/it/ideaworks/myself/my-app/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ my-app ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /Users/cosmicbugs/it/ideaworks/myself/my-app/target/classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ my-app ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/cosmicbugs/it/ideaworks/myself/my-app/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ my-app ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /Users/cosmicbugs/it/ideaworks/myself/my-app/target/test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ my-app ---
[INFO] Surefire report directory: /Users/cosmicbugs/it/ideaworks/myself/my-app/target/surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.mycompany.app.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.004 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ my-app ---
[INFO] Building jar: /Users/cosmicbugs/it/ideaworks/myself/my-app/target/my-app-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.881 s
[INFO] Finished at: 2017-03-19T22:45:08+08:00
[INFO] Final Memory: 13M/245M
[INFO] ------------------------------------------------------------------------
Maven最后显示BUILD SUCCESS,执行命令成功。生成的jar包会在外面项目目录下
my-app/target/my-app-1.0-SNAPSHOT.jar
到jar包目录下执行java命令,运行jar包
java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App
得到结果如下
Hello World!
到此Maven简单创建一个Java项目并运行到此结束,大家看了这些可能对Maven有了点概念。
首先先本地系统中有Java环境,然后安装Maven、执行
下面一篇开始介绍Maven项目目录结构、pom.xml文件常用元素含义等,可以理解为简单的入门介绍。
本人所写文章没有很准确或者高雅的文字,都是大白话,如果有读者觉得写的很low请多多指教,如果觉得实在看不下去,请关闭此页面重新换一个新博客。