MAVEN 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/xsd/maven-4.0.0.xsd">
<!-- POM版本,4.0.0是唯一支持MAVEN 2和3的POM版本 -->
<modelVersion>4.0.0</modelVersion>
<!-- 组织id,唯一的 -->
<groupId>com.loge</groupId>
<!-- 项目id -->
<artifactId>utils</artifactId>
<!-- 项目版本 -->
<version>0.0.1-SNAPSHOT</version>
<!-- 项目打包类型 -->
<packaging>jar</packaging>
<!-- 项目别称,非必须 -->
<name>my-project</name>
<!-- 项目描述,非必须 -->
<description>这个是项目的描述!</description>
<!-- 项目url,非必须 -->
<url>http://XXX.XXX</url>
<!-- MAVEN属性是值占位符,可以使用符号${X}(其中X是属性)在POM中的任何位置访问它们的值。或者它们可以用作插件的默认值。-->
<properties>
<!-- log4j日志文件管理包版本 -->
<slf4j.version>1.7.30</slf4j.version>
<log4j.version>1.2.17</log4j.version>
<!-- fast json版本 -->
<fastjson.version>1.2.47</fastjson.version>
<!-- junit.version版本 -->
<junit.version>4.12</junit.version>
<commons-lang3.version>3.4</commons-lang3.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties><!-- 上传jar包到maven私服,需要配合maven中setting的配置使用
比如配置了:
<server>
<id>nexus-releases-version</id>
<username>admin</username>
<password>admin</password>
</server>
<server>
<id>nexus-snapshots-version</id>
<username>dev</username>
<password>dev</password>
</server>
<mirrors>
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://maven.XXX.com:8888/repository/maven-public/</url>
</mirror>
</mirrors>
最后,以eclipse为例,右键项目的pom.xml文件中右击,run As – Maven build …
在弹出对话框中输入deploy -e (其中-e是再出现错误是打印完整的stack trace),将构建到本地和远程私服上
<distributionManagement>
<repository>
<id>nexus-releases-version</id>
<url>http://maven.XXX.com:8888/repository/maven-releases-version/</url>
</repository>
<snapshotRepository>
<id>nexus-snapshots-version</id>
<url>http://maven.XXX.com:8888/repository/maven-snapshots-version/</url>
</snapshotRepository>
</distributionManagement>
-->
<!-- 通过它元素来管理jar包的版本,让子项目中引用一个依赖而不用显示的列出版本号。Maven会沿着父子层次向上走,直到找到一个拥有dependencyManagement元素的项目,然后它就会使用在这个dependencyManagement元素中指定的版本号 -->
<dependencyManagement>
<dependencies>...</dependencies>
</dependencyManagement>
<dependencies>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<!-- 表示开发的时候引入,发布的时候不会加载此包 -->
<scope>test</scope>
</dependency>
<!-- 日志文件管理包 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
<!-- 作用范围:有编译、运行、测试编译、测试运行、打包阶段。 -->
<!-- scope的值有:compile、provided、runtime、test、system -->
<!-- compile:scope默认值。参与到编译、运行、测试编译、测试运行阶段,会被打包,该依赖关系会传播到依赖项目。 -->
<!-- provided:参与到编译、测试编译阶段,不会被打包。该依赖关系不具有传递性。 -->
<!-- runtime:参与到运行、测试运行阶段,会被打包。 -->
<!-- test:参与到测试编译、测试运行阶段。不会被打包,它不具有传递性。 -->
<!-- system: 类似provided,不过依赖不会从maven远程中央仓库下载,而是从本地maven仓库中获取。-->
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- json -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<!-- common工具类包 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
</dependencies>
<!-- 项目构建相关 -->
<build>
<!-- 执行build任务时,如果没有指定目标,将使用的默认值。如下配置:在命令行中执行mvn,则相当于执行mvn compile,否则默认idea会报错-->
<defaultGoal>compile</defaultGoal>
<!-- 项目构建时的名称 -->
<finalName>project1</finalName>
<plugins>
<plugin>
<!-- maven的默认编译使用的jdk版本貌似很低,使用maven-compiler-plugin插件可以指定项目源码的jdk版本,编译后的jdk版本,以及编码 -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<encoding>UTF-8</encoding>
<fork>true</fork>
</configuration>
</plugin>
<plugin>
<!-- 用Maven针对不同的环境来打包
<groupId>com.juvenxu.portable-config-maven-plugin</groupId>
<artifactId>portable-config-maven-plugin</artifactId>
<version>1.1.4</version>
<executions>
<execution>
<goals>
<goal>replace-package</goal>
</goals>
</execution>
</executions>
<configuration>
<portableConfig>src/main/portable/test.xml</portableConfig>
</configuration>-->
</plugin>
<!--添加该plugin,打带源码的jar包-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.java</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
MAVEN的build标签摘录网络:
作用
1,使用maven构建的项目可以直接使用maven build完成项目的编译、测试、打包,无需额外配置。,2,build标签描述了如何编译及打包项目,具体的编译和打包工作是通过其中的plugin配置来实现的。当然,plugin不是必须的,即使不添加默认也会引入以下插件:
3 如果有需要可以另外进行手动配置了编译时使用的jdk版本。
build
分类:一种是直接写在下,即project build。另一种是直接写在下,即profile build。
前者包含了build的基本元素和<…Directory>和,后者指包括基本元素。
三个基本元素
defaultGoal:执行构建(即编译或打包吧)时默认的goal或phase,如jar:jar或者package。
directory:构建的结果所在的路径,默认为${basedir}/target目录。
finalName:打包文件名。
resources元素:资源往往不是代码,而是properties或xml文件,无需编译,构建过程中往往会将资源文件从源路径复制到指定的目标路径,resources则给出各个资源在maven项目中的具体路径。
targetPath:资源文件的目标路径。
filtering:构建过程中是否对资源进行过滤,默认false。
directory:资源文件源路径,默认位于${basedir}/src/main/resources/目录下。
includes:一组文件名的匹配模式,被匹配的资源文件将被构建过程处理。
excludes:一组文件名的匹配模式,被匹配的资源文件将被构建过程忽略。同时也被includes的文件依然被忽略。
filters:给出对资源文件进行过滤的属性文件的路径,默认位于${basedir}/src/main/filters/目录下。属性文件中定义若干了键值对,用于在构建过程中将资源文件中出现的变量(键)替换为对应的值。
testResources:test过程中涉及的资源文件,默认位于${basedir}/src/test/resources/目录下,它们不会被构建到目标构件中。
plugins:设置构建过程中需要的插件。
extensions:是否加载该插件的扩展,默认false。
inherited: 该插件的configuration中的配置是否可以被继承(继承该pom中的其他maven项目),默认true。
configuration:该插件所需要的特殊配置,在父子项目之间可以覆盖或合并。
dependencies: 该插件所需要的依赖类库。
executions: 该插件的某个goal的执行方式。一个executions有如下属性:
id: 唯一标识。
goals:要执行的插件的goal,如run
phase: 插件的goal要嵌入到Maven的phase中执行,如verify
inherited: 该execution是否可被子项目继承
configuration:该execution的其他配置参数
…Directory: 往往配置在父项目中,供所有父子项目使用。目录可以使用绝对路径,如下图所示,若使用相对路径,则都是在${basedir}目录下。
extensions:执行构建过程中可能用到的其他工具,在执行过程中被加入到classpath中;也可以**构建插件,从而改变构建的过程。通常通过它给出通用插件的一个具体实现,用于构建过程。
编译后文件的存放目录
即maven默认的输入输出目录。
src/main/java和src/test/java:这两个目录中的所有*.java文件会分别在comile和test-comiple阶段被编译,编译结果分别放到了target/classes和targe/test-classes目录中,但是这两个目录中的其他文件都会被忽略掉。
src/main/resouces和src/test/resources:这两个目录中的文件也会分别被复制到target/classes和target/test-classes目录中。
当是web项目时,会在target下生成myproject目录,myproject是你的项目名。
src/main/webapps:这个目录中的文件会被复制到target/myProject目录中.
target/classes:默认会把这个目录中的所有内容复制到target/myProject/WEB-INF/classes目录中.
dependency: 默认会将项目的依赖复制到target/myProject/WEB-INF/lib.