使用maven编译Java项目 http://www.tuicool.com/articles/YfIfIrq
使用maven编译Java项目
综述
本文演示了用Maven编译Java项目
需要
- 时间:15分钟
- 文本编辑器或者IDE
- JDK 6 或者更高版本
创建项目
本例主要为了展示Maven,所以Java的项目力求简单。
创建项目结构
选择一个项目目录,在 *nix系统上使用下面语句
mkdir -p src/main/java/hello
window下使用命令
mkdir src\main\java\hello
创建如下结构:
└── src
└── main
└── java
└── hello
在 src/main/java/hello 目录下创建Java文件 HelloWorld.java 和 Greeter.java
src/main/java/hello/HelloWorld.java
package hello;
public class HelloWorld {
public static void main(String[] args) {
Greeter greeter = new Greeter();
System.out.println(greeter.sayHello());
}
}
src/main/java/hello/Greeter.java
package hello;
public class Greeter {
public String sayHello() {
return "Hello world!";
}
}
现在项目完成,可以用Maven编译了。有关Maven的安装,可以参考 Apache Maven 3.1.0 安装、部署、使用
定义简单的Maven编译
首先,在项目的根目录下创建一个Maven项目定义文件 pom.xml ,该文件主要是说明项目的名称、版本和依赖库
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>org.springframework</groupId>
<artifactId>gs-maven</artifactId>
<packaging>jar</packaging>
<version>0.1.0</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>hello.HelloWorld</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
除了<packaging> 元素可选外,其他的元素是构成 pom.xml 文件的最基本的元素了。它包括以下几个项目的配置:
- <modelVersion> :POM 模块版本(通常是4.0.0).
- <groupId> :项目所属的组织编号,通常用域名
- <artifactId> 项目的名称(比如,JAR或者WAR的名称)
- <version> 项目编译的版本号
- <packaging> 项目打包形式,jar或者war
编译Java代码
运行下面语句编译
mvn compile
编译完成的 .class 文件将会出现在 target/classes 目录下.如下图
运行项目:
mvn exec:java -Dexec.mainClass="hello.HelloWorld"
输出如下:
如果不想直接运行 .class 文件,可以将其打包:
mvn package
打包完成后,会在 target 目录下生成一个JAR文件,文件名由<artifactId> 和 <version>组成。比如本例,将会根据 pom.xml 生成 gs-maven-0.1.0.jar.
如果你想安装您的项目的JAR文件到本地Maven仓库,那么你应该调用下面语句:
mvn install
此时,你的项目代码将会经过编译、测试、打包并拷贝到本地依赖库,提供给其他项目引用。
说到项目依赖,下面说下 声明依赖
声明依赖
上面的例子比较简单,没有用到其他库。但是真实的项目可能会引用(依赖)到很多其他库。
下面例子,依赖了 Joda Time 的库。
修改 src/main/java/hello/HelloWorld.java
package hello;
import org.joda.time.LocalTime;
public class HelloWorld {
public static void main(String[] args) {
LocalTime currentTime = new LocalTime();
System.out.println("The current local time is: " + currentTime);
Greeter greeter = new Greeter();
System.out.println(greeter.sayHello());
}
}
现在运行 mvn compile 将会报错,因为没有声明依赖。在 <project> 节点下插入如下:
<dependencies>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
这段内容就声明了项目的依赖。每个依赖节点<dependency>都由三个子节点组成:
- <groupId> : 该依赖库所属的组织名称
- <artifactId> : 依赖的库名
- <version> : 依赖的库版本
在POM 4中,<dependency> 中还引入了<scope> ,它主要管理依赖的部署。目前<scope> 可以使用5个值:
- compile,缺省值,适用于所有阶段,会随着项目一起发布。
- provided,类似compile,期望JDK、容器或使用者会提供这个依赖。如servlet.jar。
- runtime,只在运行时使用,如JDBC驱动,适用运行和测试阶段。
- test,只在测试时使用,用于编译和运行测试代码。不会随项目发布。
- system,类似provided,需要显式提供包含依赖的jar,Maven不会在Repository中查找它。
现在你运行 mvn compile 或者 mvn package ,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>org.springframework</groupId>
<artifactId>gs-maven</artifactId>
<packaging>jar</packaging>
<version>0.1.0</version>
<!-- tag::joda[] -->
<dependencies>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
<!-- end::joda[] -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>hello.HelloWorld</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
参考: http://spring.io/guides/gs/maven/
http://www.vineetmanohar.com/2009/11/3-ways-to-run-java-main-from-maven/
使用maven编译Java项目 http://www.tuicool.com/articles/YfIfIrq的更多相关文章
- 使用maven编译Java项目
摘要: 综述 本文演示了用Maven编译Java项目 需要 时间:15分钟 文本编辑器或者IDE JDK 6 或者更高版本 创建项目 本例主要为了展示Maven,所以Java的项目力求简单. 创建项目 ...
- Maven编译Java项目
Spring在线参考文档: http://spring.io/guides/gs/maven/ 下载安装 Downloadand unzip the source repository for thi ...
- Jenkins 通过 maven 构建编译 JAVA 项目环境
Jenkins 通过maven 构建编译 JAVA 项目环境 官网下载合适Jenkins版本包: 1.jenkins http://mirrors.jenkins.io/war-stable/ 2.J ...
- maven构建java项目、web项目
maven构建java项目.web项目 一.mvn构建web项目 1安装mvn(包括path) 2命令:mvn archetype:create -DgroupId=cn.edu.sdau.neat ...
- maven 学习---使用Maven创建Java项目
在本教程中,我们将向你展示如何使用 Maven 来创建一个 Java 项目,导入其到Eclipse IDE,并打包 Java 项目到一个 JAR 文件. 所需要的工具: Maven 3.3.3 Ecl ...
- 《Maven在Java项目开发中的应用》论文笔记(十七)
标题:Maven在Java项目开发中的应用 一.基本信息 时间:2019 来源:山西农业大学 关键词:Maven:Java Web:仓库:开发人员:极限编程; 二.研究内容 1.Maven 基本原理概 ...
- appium + maven +jenkins 基本入门之二 新建maven 的java项目
1: 下载maven : 1.0 :设置maven的环境变量: 1.1: 设置maven本地仓库: 在下载好的maven文件夹找到 apache-maven-3.3.9/conf 文件夹下的setti ...
- 使用 Gradle 编译 Java 项目时报错: Could not find Tools.jar
在使用Android studio进行编译成jar的时候,遇到Gradle 编译错误,听前辈们说是jdk的版本不对,于是乎就更新了一下jdk, 然而可能是我重新安装jdk的时候改变了安装路径, 在pr ...
- ANT入门&用ANT编译java项目
第一次接触ant是15年在无锡某软件公司实习时,当时的项目是由多个模块组成,开发分成模块开发的几个小组.为了提高开发效率,采用这种编译项目的方法. 最近接触到flex项目,采用eclipse自动编译的 ...
随机推荐
- hoj2662 状态压缩dp
Pieces Assignment My Tags (Edit) Source : zhouguyue Time limit : 1 sec Memory limit : 64 M S ...
- Css-控制div斜转
必须放在css声明中 div { transform: rotate(45deg); -o-transform: rotate(45deg); float: right; -webkit-transf ...
- EventBus完全解析--组件/线程间通信利器
github地址:https://github.com/greenrobot/EventBus 1, Android EventBus实战, 没听过你就out了 2, Android EventBu ...
- 【caffe】mnist训练日志
@tags caffe 前面根据train_lenet.sh改写了train_lenet.py后,在根目录下执行它,得到一系列输出,内容如下: I1013 10:05:16.721294 1684 c ...
- 【BZOJ-1857】传送带 三分套三分
1857: [Scoi2010]传送带 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 1077 Solved: 575[Submit][Status][ ...
- POJ3154 Graveyard
Graveyard Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 1654 Accepted: 840 Specia ...
- HC-05初探
catalogue . 蓝牙嗅探抓包 . HC05蓝牙模块AT模式设置 . USB转串口芯片CH340 . 蓝牙小车 1. 蓝牙嗅探抓包 针对蓝牙通信包的嗅探抓包不能直接使用wincap+wiresh ...
- Ninject.MVC 知识点记录
Ninject 是跟Unity 差不多的DI容器.Ninject 推荐零配置,快速使用.小中型项目,最适合. 通过nuget,安装Ninject.MVC.略.参考博客:Ninject依赖注入 ...
- HDU 2795 Billboard(线段树)
题目链接: 传送门 Billboard Time Limit: 2000MS Memory Limit: 32768 K Description At the entrance to the ...
- 三角形问题的解决复杂度O(n^3)和O(nlogn)的比较
问题描述: n条棍子组成一个三角形,使得三角形周少最大. 方法一: 暴力解则算法复杂度为O(n^3) #include<stdio.h> const int MAX_N=105 int m ...