Maven工程配置

================ 基本配置 ================

1. 设置构建出的程序包名称

Maven构建出的程序包,无论是jar, war, 或是ear也好,名称默认都是由${artifactId}-${version}组成的,如:seurat-web-1.0.0-SNAPSHOT.war

如果希望设置生成的程序包名称,如seurat-web.war,可以在<build>下的<finalName>中指定:

<build>
	<finalName>seurat-web</finalName>
</build>

================ Maven构建插件 ================

1. 代码编译

<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>

编译插件用于编译工程中的Java源码,默认编译器使用JDK javac,并默认允许的Java源码和输出的编译版本都是1.5。

如果编写的源码是基于jdk1.6的,则需要明确配置插件的source和target属性,如下:

<plugin>
	<artifactId>maven-compiler-plugin</artifactId>
	<configuration>
		<source>1.6</source>
		<target>1.6</target>
	</configuration>
</plugin>

Note:

  • 在pom中,Apache的Maven插件可以不用显示指定<groupId>
  • 在导入Maven项目时,Eclipse会根据compiler插件上source属性指定的JDK版本,准备相应的JDK构建环境。

更多参数配置见:Maven Compiler Plugin

http://maven.apache.org/plugins/maven-compiler-plugin/

上述插件的source和target属性,直接对应jdk javac的两个命令行选项,具体含义如下:

-source release

Specifies the version of source code accepted. The following values for release are allowed:

1.3  The compiler does not support assertions, generics, or other language features introduced after JDK 1.3.

   1.4  The compiler accepts code containing assertions, which were introduced in JDK 1.4.

   1.5  The compiler accepts code containing generics and other language features introduced in JDK 5.

   5     Synonym for 1.5.

   1.6  This is the default value. No language changes were introduced in Java SE 6. However, encoding errors in source files are now reported as errors, instead of warnings, as previously.

   6     Synonym for 1.6.

-target version

Generate class files that target a specified version of the VM. Class files will run on the specified target and on later versions, but not on earlier versions of the VM. Valid targets are 1.1 1.2 1.3 1.4 1.5 (also 5) and 1.6 (also 6).

The default for -target depends on the value of -source:

If -source is not specified, the value of -target is 1.6

If -source is 1.2, the value of -target is 1.4

If -source is 1.3, the value of -target is 1.4

For all other values of -source, the value of -target is the value of -source.

更详细信息可参考JDK javac: http://docs.oracle.com/javase/6/docs/technotes/tools/windows/javac.html

何时利用source和target?

利用source和target选项,允许由低版本的java源代编译出高版本的class文件,反之不行。

如用JDK javac 1.6,可以将JDK 1.4标准编写的源码编译为符合JDK 1.6规范的class文件,但用1.6写的代码,由于引入了更多的语言特性,无法编译出1.4规范的class文件。

如何查看.class文件的JDK编译版本?

编译后的class类文件版本,可以通过文件头来识别。用二进制编辑器打开.class文件,第一行内容如:

CA FE BA BE 00 00 00 32 01 E7 09 00 04 01 15 0A

前四个字节为固定的 CA FE BA BE ,接下来的四个字节为次版本号(0000)和主版本号(0032),其中主版本号的位置代表了JDK编译版本:
32:JDK1.6 
31:JDK1.5 
30:JDK1.4 
2F:JDK1.3 

2. 拷贝资源文件(并过滤资源实现变量替换)

<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>

该插件主要用于将工程下的资源文件拷贝到编译代码的输出目录下,并可以设置激活资源过滤实现变量替换。

Note:

默认情况下,对资源过滤及替换变量的功能是关闭的,需要时必须激活才能生效。

如何激活资源过滤功能,进行变量替换?

在<build>的资源目录配置中,增加资源过滤<filter>属性并设置为true,具体如下:

<build>
	<resources>
		<resource>
			<directory>src/main/resources</directory>
			<filtering>true</filtering>
		</resource>
	</resources>
	<testResources>
		<testResource>
			<directory>src/test/resources</directory>
			<filtering>true</filtering>
		</testResource>
	</testResources>
</build>
  • 不设<filter>值则默认为false,Resource插件只进行资源文件拷贝,而不做资源过滤,也不进行资源文件中的变量替换工作。
  • <directory>用于设置资源文件的根目录。

关于编码集

对资源进行过滤时,插件默认会按project.build.sourceEncoding变量设置编码集读入资源文件,进行变量替换,最后按该编码集写入到输出目录,所以资源文件的编码集要和这个变量指定的编码集兼容。

如果没有指定project.build.sourceEncoding变量的编码集,那么操作系统级的编码集将作为该变量值,这样可能会导致资源文件过滤后乱码的问题。如资源文件编码用的是UTF-8, project.build.sourceEncoding变量是GBK,则插件将会用GBK解析资源文件,导致输出目录下的资源文件乱码。

因此,最好显示指定project.build.sourceEncoding的编码集,或利用插件的<encoding>配置指定资源过滤时使用的编码集。

3. 可执行jar打包插件

<plugin>

        <artifactId>maven-assembly-plugin</artifactId>

        <version>2.4.1</version>

        <configuration>

          <archive>

            <manifest>

              <mainClass>cn.david.freemaker.jbosseap6.JBossEap6Generator</mainClass>

            </manifest>

          </archive>

          <descriptorRefs>

            <descriptorRef>jar-with-dependencies</descriptorRef>

          </descriptorRefs>

        </configuration>

      </plugin>

执行Maven命令:mvn assembly:assembly

猜你喜欢

转载自yyjlinux.iteye.com/blog/1691571
今日推荐