您的位置:首页 > 编程语言 > Java开发

创建基于maven的java project

2014-11-25 10:51 302 查看

前提条件

首先要确保你的电脑已经正确安装了java&maven,在本文中,java的版本是1.7;maven的版本是3.2.3。

使用maven命令创建项目

在命令行窗口,键入如下代码:

mvn archetype:generate

回车之后,命令行会有类似输出:

...

[INFO] Generating project in Interactive mode (这里会卡一会儿,因为要联网获取项目模板)

[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)

Choose archetype:

1: remote -> br.com.ingenieux:elasticbeanstalk-service-webapp-archetype (A Maven Archetype Encompassing RestAssured, Jetty, Jackson, Guice and Jersey for Publishing JAX-RS-based Services on AWS' Elastic Beanstalk Service)

... (这里会自动列出很多项目模板,每种模板前面会有一个数字序号)

336: remote -> org.apache.maven.archetypes:maven-archetype-quickstart (An archetype which contains a sample Maven project.)

...

Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 336: (这里根据每个人机器上安装的eclipse插件不同,可能默认的数字不是这个,先不管,直接回车)

Choose org.apache.maven.archetypes:maven-archetype-quickstart version:

1: 1.0-alpha-1

2: 1.0-alpha-2

3: 1.0-alpha-3

4: 1.0-alpha-4

5: 1.0

6: 1.1

Choose a number: 6: (直接回车)

Define value for property 'groupId': : com.helloworld (可暂时先理解成类似package或namespace的名称,通常我们填写组织机构名称缩写)

Define value for property 'artifactId': : helloworld (组件名称,可暂时理解成项目名称)

Define value for property 'version': 1.0-SNAPSHOT: : (版本号,直接回车,默认1.0-SNAPSHOT)

Define value for property 'package': com.helloworld: : (打包后的jar文件名,相当于.net中项目最后生成的程序集dll名称)

Confirm properties configuration:

groupId: com.helloworld

artifactId: helloworld

version: 1.0-SNAPSHOT

package: com.helloworld

Y: : (直接回车确认)


[INFO] ----------------------------------------------------------------------------

[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.1

[INFO] ----------------------------------------------------------------------------

[INFO] Parameter: groupId, Value: com.helloworld

[INFO] Parameter: packageName, com.helloworld

[INFO] Parameter: package, Value: com.helloworld

[INFO] Parameter: artifactId, Value: helloworld

...

[INFO] ------------------------------------------------------------------------

[INFO] BUILD SUCCESS (看到这个,表示项目创建成功!)

[INFO] ------------------------------------------------------------------------

...

maven项目的目录结构

在命令行运行如下命令:

D:\helloworld>tree

可以看到maven的项目结构如下:

D:.

└─src

├─main

│ └─java

│ └─com

│ └─helloworld

└─test

└─java

└─com

└─helloworld


编译maven项目

在项目根目录下执行:

mvn clean compile

如果看到类似下面的输出,则表示编译成功:

D:\helloworld>mvn clean compile

[INFO] Scanning for projects...

[INFO]

[INFO] ------------------------------------------------------------------------

[INFO] Building helloworld 1.0-SNAPSHOT

[INFO] ------------------------------------------------------------------------

[INFO]

[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ helloworld ---

[INFO]

[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ helloworld

---

[INFO] Using 'UTF-8' encoding to copy filtered resources.

[INFO] skip non existing resourceDirectory D:\helloworld\src\main\resources

[INFO]

[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ helloworld ---

[INFO] Changes detected - recompiling the module!

[INFO] Compiling 1 source file to D:\helloworld\target\classes

[INFO] ------------------------------------------------------------------------

[INFO] BUILD SUCCESS

[INFO] ------------------------------------------------------------------------

[INFO] Total time: 1.365 s

[INFO] Finished at: 2014-11-25T10:38:05+08:00

[INFO] Final Memory: 7M/17M

[INFO] ------------------------------------------------------------------------

D:\helloworld>

打包maven项目

通常我们会把java项目打包成jar包或war包,maven中打包的命令为

mvn clean package

运行完后,会在target目录下生成jar包

注:从输出 可以发现,package前,会先执行compile,再执行test,最后才是package打包

运行maven项目

该项目中的App.java中有main方法,可以直接运行,常规方式下,我们如果想直接运行class文件,得敲一段很长的命令,maven中不必这么复杂,先用记事本打开项目根目录下的pom.xml文件,增加下面这节内容:

<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>java</executable>
<arguments>
<argument>-classpath</argument>
<classpath>
</classpath>
<argument>cnblogs.App</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>


然后在命令行下,输入

mvn exec:exec

即可运行,下面是输出:

D:\helloworld>mvn exec:exec

[INFO] Scanning for projects...

[INFO]

[INFO] ------------------------------------------------------------------------

[INFO] Building helloworld 1.0-SNAPSHOT

[INFO] ------------------------------------------------------------------------

[INFO]

[INFO] --- exec-maven-plugin:1.2.1:exec (default-cli) @ helloworld ---

Hello World!

[INFO] ------------------------------------------------------------------------

[INFO] BUILD SUCCESS

[INFO] ------------------------------------------------------------------------

[INFO] Total time: 0.612 s

[INFO] Finished at: 2014-11-25T10:48:18+08:00

[INFO] Final Memory: 2M/15M

[INFO] ------------------------------------------------------------------------

D:\helloworld>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: