综述
本文演示了用maven编译java项目
需要
创建项目
本例主要为了展示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 文件的最基本的元素了。它包括以下几个项目的配置:
编译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 installmvn install
此时,你的项目代码将会经过编译、测试、打包并拷贝到本地依赖库,提供给其他项目引用。
以上例子源码的地址中的demo1
说到项目依赖,下面说下声明依赖
声明依赖
上面的例子比较简单,没有用到其他库。但是真实的项目可能会引用(依赖)到很多其他库。
下面例子,依赖了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>都由三个子节点组成:
在pom 4中,<dependency> 中还引入了<scope> ,它主要管理依赖的部署。目前<scope> 可以使用5个值:
现在你运行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>
运行项目:
mvn exec:java -dexec.mainclass="hello.helloworld"
输出如下:
以上例子源码的地址中的demo2
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。
如对本文有疑问, 点击进行留言回复!!
Mybatis PersistenceException异常:Error building SqlSession The error may exist in SQL Mapper Configur
MyBatis——动态SQL语句——if标签和where标签复合使用
网友评论