Maven
来源:互联网 发布:windows ad域安装 编辑:程序博客网 时间:2024/05/02 12:36
In this tutorial, we will show you how to create a Java project with Maven3, imports it into the Eclipse IDE, and package the Java project into a jar faile.
Tools used:
- Maven 3.3.1
- Eclipse 4.4
- JDK 7
<strong>Note</strong>Please make sure Maven is installed and configured properly before you proceed with this tutorial, to avoid mvn command not found error.
1. Create a project from maven template
In a terminal(Unix or Linux) or command prompt(Windows), navigate to the folder you want to create the Java project. Type this command:
mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart
<span style="white-space:pre"></span>-DgroupId={project-packging}
<span style="white-space:pre"></span>-DartifactId={project-name}
<span style="white-space:pre"></span>-DinteractiveMode=false
This tells Maven to create a Java project from the Maven maven-archetype-quickstart template. You can ignore the archetypeArtfactId option, maven will use the default template: maven-archetype-quickstart.
For example,
D:\workspaces\MavenDemo>mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=org.whl.demoDartifactId=demo -DinteractiveMode=false[INFO] Scanning for projects...[INFO][INFO] ------------------------------------------------------------------------[INFO] Building Maven Stub Project (No POM) 1[INFO] ------------------------------------------------------------------------[INFO][INFO] >>> maven-archetype-plugin:2.3:generate (default-cli) > generate-sources @ standalone-pom >>>[INFO][INFO] <<< maven-archetype-plugin:2.3:generate (default-cli) < generate-sources @ standalone-pom <<<[INFO][INFO] --- maven-archetype-plugin:2.3:generate (default-cli) @ standalone-pom ---[INFO] Generating project in Batch mode[INFO] ----------------------------------------------------------------------------[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.0[INFO] ----------------------------------------------------------------------------[INFO] Parameter: groupId, Value: org.whl.demo[INFO] Parameter: packageName, Value: org.whl.demo[INFO] Parameter: package, Value: org.whl.demo[INFO] Parameter: artifactId, Value: demo[INFO] Parameter: basedir, Value: D:\workspaces\MavenDemo[INFO] Parameter: version, Value: 1.0-SNAPSHOT[INFO] project created from Old (1.x) Archetype in dir: D:\workspaces\MavenDemo\demo[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time: 10.672 s[INFO] Finished at: 2015-05-17T20:55:24+08:00[INFO] Final Memory: 15M/152M[INFO] ------------------------------------------------------------------------
In above case, a new Java project named "demo", and the entire project directory structure is created automatically.
<strong>Note</strong>You can debug the Maven with parameter -X if you have any issues.In additional, this step is not mandatory, you can create the folders manually, see step 2 project structure.
with mvn archetype:generate+maven-archetype-quickstart template, the follwing project directory structrue is created.
demo |-src |---main |------java |--------com |----------whl |------------demo |---------------App.java |---test
</pre><pre name="code" class="plain"> |--------com |----------whl |------------demo |---------------AppTest.java
|-pom.xml
In simple, all source code puts in folders /src/main/java/, all unit test code puts in /srtc/test/java/.
In additional, a standard pom.xml is generated. This POM file is like the Ant build.xml file, it describes the entire project information, everything from directory sturctrue, project plugins, project dependencies, how to build this project and etc,read this official POM guide.
pom.xml
<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>org.whl.demo</groupId> <artifactId>demo</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>demo</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies></project>
To make this as an Eclipse project, in prompt, navigate to "demo" project, type this command:
mvn eclipse:eclipse
It will generate all project files that are required by Eclipse IDE.
To import the project into Eclipse IDE, select "File -> Import ... -> General -> Existing Projected into Workspace"
4.Update POM
The default pom.xml is too simple, often times you need to add the compiler plugin to tell Maven which JDK version is used to compile you project.(The default JDK1.4 is too old)
<plugin> <groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>2.3.2</version><configuration><source>1.7</source><target>1.7</target></configuration> </plugin>
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency>
pom.xml --updated version
<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>org.whl.demo</groupId> <artifactId>demo</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>demo</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>2.3.2</version><configuration><source>1.7</source><target>1.7</target></configuration> </plugin> </plugins> </build></project>
In prompt, issue the same command mvn eclipse:eclipse again, Maven will download the plugins and project dependencies (junit) from Maven center repository and store it into your local Maven repository automaticall.
- Maven
- Maven
- Maven
- maven
- Maven
- Maven
- maven
- maven
- maven
- maven
- Maven
- maven .
- Maven
- maven
- maven
- maven
- MAVEN
- Maven
- Animation 动画详解(一)——alpha、scale、translate、rotate、set的xml属性及用法
- 1003. Sorting
- 1626 - Brackets sequence
- 数据库笔记(自己学习过程中整理)
- honey----- jstl中<c:forEach>的用法【没有试验过里面的例子,仅供参考】
- Maven
- Python学习笔记(五)
- PL/SQL在取行、取列、取行列以及分页中的应用
- C++中虚函数的作用是什么?它应该怎么用呢?
- 【机房重构】——错误中成长
- 索引与优化like查询
- 山东省ACM竞赛(2015)---B - Lowest Unique Price
- honey-----常用标签库的语句,在jsp的开头写
- 通过HTTP协议实现文件上传