来自:http://shiyanjun.cn/archives/180.html

使用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>

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>

9,assembly报错不好用时,用下面的方法

原因:http://chenzhou123520.iteye.com/blog/1706242

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.4</version>
<configuration>
<createDependencyReducedPom>true</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass></mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Maven构建应用程序常用配置(转)的更多相关文章

  1. maven 构建spring ssh mybatis 配置

    详情参与 http://blog.csdn.net/yuguiyang1990/article/details/8811817 前面我们使用Maven构建了Struts2项目,这里我们来试一下Hibe ...

  2. Maven打包Spark程序Pom配置

    scala和java混合的spark程序之前使用其他配置始终有报找不到包的情况,尝试了一下如下配置可以打包成功.<build> <pluginManagement> <p ...

  3. maven添加镜像与常用配置

    maven解压后conf文件夹有个 settings.xml 在这个文件中可以配置我们的maven 配置镜像: 找到<mirrors></mirrors>找到这个节点在节点中添 ...

  4. 启用 Jenkins 持续构建 .NET 程序,关于配置nuget关键点

    网上关于 Jenkins + net 的文章一大堆,这里只贴出 配置中的关键点, 第一步: 从官网下载 nuget.exe 安装包进行安装, 如果项目是用 vs2017 开发的 需要特别注意,nuge ...

  5. Maven pom.xml 全配置(一)常用配置

    Maven pom.xml 全配置(一)常用配置 这里贴出一个Maven中出现频率较高的配置参数注释,方便理解项目中Maven的配置具体的作用.如果在此博文中没有找到你想看到的参数,可以移步Maven ...

  6. Maven pom.xml 全配置(二)不常用配置

    Maven pom.xml 全配置(二)不常用配置 这里贴出Maven pom.xml文件中使用率较少的配置参数,如果此篇文档中没有找到你想要的参数,移步Maven pom.xml 全配置(一)常用配 ...

  7. maven构建web项目,用jetty测试的配置pom.xml

    maven构建web项目,用jetty测试的配置pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmln ...

  8. struts 2.3.28+spring 4.2.5.RELEASE+hibernate 5.1.0.Final整合maven构建项目基本配置

    第一次写博客,主要也是记录给自己看的,可能很多比较熟的地方就没注释 用maven构建,ssh框架都是选用的最新的release版(感觉还是不要用beta),环境jdk1.8 tomcat8.0 mys ...

  9. IDEA的常用配置(Maven)一键导入及优化内存

    IDEA的常用配置一键导入 一.在https://www.cnblogs.com/zyx110/p/10799387.html中下载如图的压缩包 下载完成后解压缩,点击settings_bak,你会看 ...

随机推荐

  1. <hr> 水平样式分隔线

    <hr>  水平样式分隔线 <html> <body> <p>hr 标签定义水平线:</p> <hr /> <p>这 ...

  2. HDOJ 4869 Turn the pokers

    最后的结果中正面向上的奇偶性是一定的,计算出正面向上的范围low,up 结果即为 C(m.low)+ C(m.low+2) +.... + C(m,up) ,用逆元取模 Turn the pokers ...

  3. Swift3.0:NSURLConnection的使用

    一.介绍 应用中也不必不可少的会使用网络通信,增强客户端和服务器的交互,可以使用NSURLConnection实现http通信. NSURLConnection提供了异步请求和同步请求两种请求方式.同 ...

  4. windows server 2012将计算机图标添加到桌面

    windows server 2012系统安装完以后桌面默认只有回收站一个图标,如何将window常用的图标(计算机.控制面板.网络.用户文件)的图标添加到桌面呢,下面为作者本人亲测.操作简单至极. ...

  5. 第零章 dubbo源码解析目录

    第一章 第一个dubbo项目 第二章  dubbo内核之spi源码解析 2.1  jdk-spi的实现原理 2.2 dubbo-spi源码解析 第三章 dubbo内核之ioc源码解析 第四章 dubb ...

  6. go语言之进阶篇 select实现的超时机制

    1.select实现的超时机制 示例: package main import ( "fmt" "time" ) func main() { ch := mak ...

  7. go语言之进阶篇通过map生成json

    1.通过map生成json 示例1: package main import ( "encoding/json" "fmt" ) func main() { / ...

  8. log4net修改数据库连接字符串和写自定义信息

    最近项目需要用log4net来写日志,因为整个平台式在sharepoint上,我们需要记录具体是哪个子站点发生的日志,因此需要再原来的log表里面添加一个自定义信息列.由于平台的安全性要求,我们需要对 ...

  9. [leetcode]Rotate Image @ Python

    原题地址:https://oj.leetcode.com/problems/rotate-image/ 题意: You are given an n x n 2D matrix representin ...

  10. PHP操作数据库函数比较

    常用的php语法连接mysql如下 <?php $link = mysql_connect('localhost', 'user', 'password'); mysql_select_db(' ...