Maven构建应用程序常用配置(Maven构建应用程序常用配置)

使用Maven来构建应用程序,可以非常方便地管理应用相关的资源众所周知,应用程序中涉及到的一些依赖关系,如Java应用程序依赖jar文件,如果只是手动找到相应的资源,可能需要花费一些时间而且,即使已经积累了库文件,在未来应用程序升级以后,还要考虑到依赖库文件的升级情况,再次搜索收集还有一个问题,对应用程序依赖文件的管理是个非常复杂工作,占用存储空间不说,还可能因为应用之间的版本问题导致依赖冲突使用Maven的pom模型来构建应用程序,可以更加有效地的管理,而且配置内容非常清晰(有时多了,可能pom文件显得有点臃肿)下面将常用的Maven配置,整理如下,以备参考首先,整理一个简单的目录,作为快速查询之用:,我来为大家科普一下关于Maven构建应用程序常用配置?下面希望有你要的答案,我们一起来看看吧!

Maven构建应用程序常用配置(Maven构建应用程序常用配置)

Maven构建应用程序常用配置

使用Maven来构建应用程序,可以非常方便地管理应用相关的资源。众所周知,应用程序中涉及到的一些依赖关系,如Java应用程序依赖jar文件,如果只是手动找到相应的资源,可能需要花费一些时间。而且,即使已经积累了库文件,在未来应用程序升级以后,还要考虑到依赖库文件的升级情况,再次搜索收集。还有一个问题,对应用程序依赖文件的管理是个非常复杂工作,占用存储空间不说,还可能因为应用之间的版本问题导致依赖冲突。使用Maven的pom模型来构建应用程序,可以更加有效地的管理,而且配置内容非常清晰(有时多了,可能pom文件显得有点臃肿)。下面将常用的Maven配置,整理如下,以备参考。首先,整理一个简单的目录,作为快速查询之用:

  1. 设置字符集
  2. 拷贝src/main/resources/资源文件
  3. 编译代码
  4. 、编译打包成jar文件
  5. 构建测试用例配置
  6. 输出依赖jar文件到指定目录
  7. 配置指定的repository
  8. 将应用及其依赖jar文件打成一个jar文件

具体配置的详细内容,如下所示:

1、设置字符集

<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties>

在需要设置字符集的地方,引用${project.build.sourceEncoding}即可。

2、拷贝src/main/resources/资源文件

<build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.5</version> <executions> <execution> <id>copy-resources</id> <phase>package</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <encoding>${project.build.sourceEncoding}</encoding> <outputDirectory>${project.build.directory}</outputDirectory> <resources> <resource> <directory>src/main/resources/</directory> <includes> <include>*.properties</include> <include>*.xml</include> </includes> <filtering>true</filtering> </resource> </resources> </configuration> </execution> </executions> </plugin> </plugins> </pluginManagement> </build>

3、编译代码

<build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.5</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> </plugins> </pluginManagement> </build>

可以指定源代码编译级别。

4、编译打包成jar文件

<build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <executions> <execution> <phase>package</phase> <goals> <goal>jar</goal> </goals> <configuration> <classifier>without-configs</classifier> <excludes> <exclude>*.properties</exclude> <exclude>*.xml</exclude> </excludes> </configuration> </execution> </executions> </plugin> </pluginManagement> </build>

可以指定打包后jar文件的文件名后缀,同时可以设置是否将配置文件也打包到jar文件中。

5、构建测试用例配置

<build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.9</version> <configuration> <skip>true</skip> <testFailureIgnore>true</testFailureIgnore> </configuration> </plugin> </plugins> </pluginManagement> </build>

构建应用时,可以配置是否执行测试用例代码,也可以配置如果测试用例未通过是否忽略。

6、输出依赖jar文件到指定目录

<build> <pluginManagement> <plugins> <plugin> <groupId>org.eclipse.m2e</groupId> <artifactId>lifecycle-mapping</artifactId> <version>1.0.0</version> <configuration> <lifecycleMappingMetadata> <pluginExecutions> <pluginExecution> <pluginExecutionFilter> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <versionRange>[2.0,)</versionRange> <goals> <goal>copy-dependencies</goal> <goal>unpack</goal> </goals> </pluginExecutionFilter> <action> <ignore /> </action> </pluginExecution> </pluginExecutions> </lifecycleMappingMetadata> </configuration> </plugin> </plugins> </pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>2.8</version> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> <overWriteReleases>false</overWriteReleases> <overWriteSnapshots>false</overWriteSnapshots> <overWriteIfNewer>true</overWriteIfNewer> </configuration> </execution> </executions> </plugin> </plugins> </build>

上面,和pluginManagement并列的plugins元素中配置的是拷贝依赖jar文件到target/lib目录下面,如果在Eclipse中出现maven-dependency-plugin (goals “copy-dependencies”, “unpack”) is not supported by m2e错误,上面pluginManagement元素中的配置,可以解决这个错误提示。

7、配置指定的repository

<repositories> <repository> <id>cloudera</id> <url>https://repository.cloudera.com/artifactory/cloudera-repos/</url> </repository> </repositories>

如果我们需要要的一些依赖jar文件在maven中央repository中没有,可以在pom文件中配置特定的repository,一般需要配置id和url。

8、将应用及其依赖jar文件打成一个jar文件

<build> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <archive> <manifest> <mainClass>org.shirdrn.solr.cloud.index.hadoop.SolrCloudIndexer</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build>

,

免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com

    分享
    投诉
    首页