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>com.juvenxu.mvnbook </groupId>
<artifactId>hello-world </artifactId>
<version>1.0-SNAPSHOT </version>
<name>Maven Hello World Project</name>
<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>com.juvenxu.mvnbook </groupId>
<artifactId>hello-world </artifactId>
<version>1.0-SNAPSHOT </version>
<name>Maven Hello World Project</name>
modelVersion指定了当前POM模型的版本,对于Maven2及Maven 3来说,它只能是4.0.0
groupId :定义项目所属组,一般定义为:com.mycom.myapp
artifactId: 定义不同模块
version: 定义版本, SNAPSHOT 表示快照版本
Maven项目中的主代码位于:
src/main/java
用Maven进行编译:
mvn clean compile
clean 告诉Maven清理输出目录target/
compile 告诉Maven编译项目主代码
测试代码放在
src/test/java
mvn clean test
添加Junit依赖
<dependencies>
< dependency>
< groupId>junit</groupId>
< artifactId>junit</artifactId>
< version>4.7</version>
< scope>test</scope>
</ dependency>
</dependencies>
< dependency>
< groupId>junit</groupId>
< artifactId>junit</artifactId>
< version>4.7</version>
< scope>test</scope>
</ dependency>
</dependencies>
scope为依赖范围,text表示依赖只对测试有效。如果没有生命scope。默认是compile
Maven的核心插件之一compiler插件默认只支持编译Java 1.3,因此我们需要配置该插件使其支持Java 5
<project>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
.…
</project>
打包
mvn clean package
安装,安装在本地目录
mvn clean install
如果想生成可运行的jar,
<!-- 打包jar文件时,配置manifest文件,加入lib包的jar依赖 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.lashou.middleware.cinema2ebs.StartSycn</mainClass>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
<manifestEntries>
<Implementation-Build>scm:${buildNumber}</Implementation-Build>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.lashou.middleware.cinema2ebs.StartSycn</mainClass>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
<manifestEntries>
<Implementation-Build>scm:${buildNumber}</Implementation-Build>
</manifestEntries>
</archive>
</configuration>
</plugin>
安装jar 到本地仓库
mvn install:install-file
-DgroupId=org.mariadb.jdbc -DartifactId=mariadb-java-client -Dversion=1.1.3 -Dpackaging=jar -Dfile=E:\Repositories\Resource\Source\mariadb-java-client-1.1.3.jar