Maven 编译 打包注意事项

1. 父子项目同时编译问题: 编译时找不到父项目的pom.xml文件

报错信息: Non-resolvable parent POM for XXXXX

解决: 在子项目pom.xml的parent中, 加入父项目的相对路径relativePath

<parent>
      <artifactId>demo-server</artifactId>
      <groupId>net.abc.demo</groupId>
      <version>1.0.0</version>
      <relativePath>../pom.xml</relativePath>
</parent>

2. 多个模块打包的依赖问题

报错信息: class net.abc.demo.XXXXX .java:[24,9] cannot find symbol: class XXX

解决: 在被依赖的模块项目pom.xml中, 加入maven-compiler-plugin插件

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <encoding>utf-8</encoding>
                    <fork>true</fork>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

如果依赖的是springboot模块, 则pom.xml为:

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <classifier>exec</classifier>
                </configuration>
            </plugin>
        </plugins>
    </build>

Maven仓库理解、如何引入本地包、Maven多种方式打可执行jar包

有关MAVEN仓库的理解参见:http://blog.csdn.net/wanghantong/article/details/36427433

MAVEN依赖关系中Scope的作用

Java代码 
  1. Dependency Scope 在POM 4中,<dependency>中还引入了<scope>,它主要管理依赖的部署。目前依赖项的作用域<scope>可以使用5个值:    
  2. 在定义项目的依赖项的时候,我们可以通过scope来指定该依赖项的作用范围。scope的取值有compile、runtime、test、provided、system和import。  
  3. compile:这是依赖项的默认作用范围,即当没有指定依赖项的scope时默认使用compile。compile范围内的依赖项在所有情况下都是有效的,包括运行、测试和编译时。  
  4. runtime:表示该依赖项只有在运行时才是需要的,在编译的时候不需要。这种类型的依赖项将在运行和test的类路径下可以访问。  
  5. test:表示该依赖项只对测试时有用,包括测试代码的编译和运行,对于正常的项目运行是没有影响的。  
  6. provided:表示该依赖项将由JDK或者运行容器在运行时提供,也就是说由Maven提供的该依赖项我们只有在编译和测试时才会用到,而在运行时将由JDK或者运行容器提供。  
  7. system:当scope为system时,表示该依赖项是我们自己提供的,不需要Maven到仓库里面去找。指定scope为system需要与另一个属性元素systemPath一起使用,它表示该依赖项在当前系统的位置,使用的是绝对路径。  

 

Java代码 
  1. POM文件里面可以引用一些内置属性(Maven预定义可以直接使用)  
  2. ${basedir} 项目根目录   
  3. ${version}表示项目版本;  
  4. ${project.basedir}同${basedir};  
  5. ${project.version}表示项目版本,与${version}相同;  
  6. ${project.build.directory} 构建目录,缺省为target  
  7. ${project.build.sourceEncoding}表示主源码的编码格式;  
  8. ${project.build.sourceDirectory}表示主源码路径;  
  9. ${project.build.finalName}表示输出文件名称;  
  10. ${project.build.outputDirectory} 构建过程输出目录,缺省为target/classes  

 

如何在Maven项目中引入本地包呢?

比如我从其它项目打一个jar包,引入到现有项目中。

方法一:将待引入的包放在目录下如lib目录下,修改pom文件,加入依赖并且scope要设置为system

 

Java代码 
 <dependency>
        <groupId>org.wltea.ik-analyzer</groupId>
        <artifactId>ik-analyzer</artifactId>
        <version>3.2.8</version>
        <scope>system</scope>
        <systemPath>${project.basedir}/lib/fbcds.jar</systemPath>
    </dependency>


 上面设置完成后,运行mvn package命令执行成功。但打出来的包里面不包含lib目录和fbcds.jar这个引用的包,即打出来的包不是可执行的jar。所以个人开发的话可以使用这种方式,如果团队开发请使用方法二。

 

方法二:将待引入的jar包安装到本地repository中

1、先把待引入的jar包放在一个目录下,需要改一下包名,如fbcds.jar修改成fbcds-1.0.jar,如F:\lib目录,在命令行CD到lib目录,执行以下命令:

Java代码 
  1. mvn install:install-file -Dfile=fbcds-1.0.jar -DgroupId=fbcds -DartifactId=fbcds -Dversion=1.0 -Dpackaging=jar  
  2. mvn install:install-file -Dfile=ojdbc7-1.0.jar -DgroupId=ojdbc7 -DartifactId=ojdbc7 -Dversion=1.0 -Dpackaging=jar  

 2、修改项目pom文件加入包对应的依赖

 

Java代码 

 

<dependencies>  

    <dependency>  

      <groupId>log4j</groupId>  

      <artifactId>log4j</artifactId>  

      <version>1.2.17</version>  

    </dependency>  

    <dependency>  

      <groupId>fbcds</groupId>  

      <artifactId>fbcds</artifactId>  

      <version>1.0</version>  

    </dependency>  

    <dependency>   

      <groupId>ojdbc7</groupId>  

      <artifactId>ojdbc7</artifactId>  

      <version>1.0</version>   

    </dependency>  

  </dependencies>  

 上面的fbcds和ojdbc7就是新加的引用包的依赖。

 

MAVEN如何打可执行的JAR包

前提条件:已成功将待引入的jar包安装到本地repository中

方法一、使用maven-shade-plugin插件打可执行的jar包

插件查找链接:http://maven.apache.org/plugins/

1、测试类代码

 

Java代码:

package com.lwf.test;  

  

import java.sql.Connection;  

import java.sql.DriverManager;  

import java.sql.ResultSet;  

import java.sql.SQLException;  

import java.sql.Statement;  

  

import com.eclink.fbcis.store.StoreDao;  

  

public class TestClass {  

    public static void main(String[] args) {  

        …….

    }  

}  

 上面类中引用到了fbcds和ojdbc7包的内容。

2、对应pom文件

Java代码 

<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”>  

  <modelVersion>4.0.0</modelVersion>  

  <groupId>222</groupId>  

  <artifactId>222</artifactId>  

  <version>0.0.1-SNAPSHOT</version>  

  <name>222</name>  

  <dependencies>  

    <dependency>  

      <groupId>log4j</groupId>  

      <artifactId>log4j</artifactId>  

      <version>1.2.17</version>  

    </dependency>  

    <dependency>  

      <groupId>fbcds</groupId>  

      <artifactId>fbcds</artifactId>  

      <version>1.0</version>  

    </dependency>  

    <dependency>   

      <groupId>ojdbc7</groupId>  

      <artifactId>ojdbc7</artifactId>  

      <version>1.0</version>   

    </dependency>  

  </dependencies>  

    <build>  

        <plugins>  

            <plugin>  

                <groupId>org.apache.maven.plugins</groupId>  

                <artifactId>maven-shade-plugin</artifactId>  

                <version>2.4.3</version>  

                <executions>  

                    <execution>  

                        <phase>package</phase>  

                        <goals>  

                            <goal>shade</goal>  

                        </goals>  

                        <configuration>  

                            <transformers>  

                                <transformer implementation=“org.apache.maven.plugins.shade.resource.ManifestResourceTransformer”>  

                                    <mainClass>com.lwf.test.TestClass</mainClass>  

                                </transformer>  

                            </transformers>  

                        </configuration>  

                    </execution>  

                </executions>  

            </plugin>  

        </plugins>  

    </build>  

  

</project>  

 在eclipse中右键项目run as 选择Maven package,可看打包的target目录内容:



 比较两个包内容:



 original-MavenPackage-0.0.1-SNAPSHOT.jar中没有主清单属性是执行不了的。

 参见:http://www.mkyong.com/maven/create-a-fat-jar-file-maven-shade-plugin/

方法二、使用maven-assembly-plugin插件打可执行的jar包

测试类与方法一中一样,只是pom不一样,pom文件如下:

Java代码 

<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”>  

  <modelVersion>4.0.0</modelVersion>  

  <groupId>com.lwf.MavenPackage</groupId>  

  <artifactId>MavenPackage</artifactId>  

  <version>0.0.1-SNAPSHOT</version>  

  <name>MavenPackage</name>  

  <dependencies>  

    <dependency>  

      <groupId>log4j</groupId>  

      <artifactId>log4j</artifactId>  

      <version>1.2.17</version>  

    </dependency>  

    <dependency>  

      <groupId>fbcds</groupId>  

      <artifactId>fbcds</artifactId>  

      <version>1.0</version>  

    </dependency>  

    <dependency>   

      <groupId>ojdbc7</groupId>  

      <artifactId>ojdbc7</artifactId>  

      <version>1.0</version>   

    </dependency>  

  </dependencies>  

    <build>  

        <plugins>          

<!– 使用 maven-Assembly-plugin插件打可执行包–>  

            <plugin>  

                <groupId>org.apache.maven.plugins</groupId>  

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

                <version>2.6</version>  

                <configuration>  

                    <!– get all project dependencies –>  

                    <descriptorRefs>  

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

                    </descriptorRefs>  

                    <!– MainClass in mainfest make a executable jar –>  

                    <archive>  

                      <manifest>  

                        <mainClass>com.lwf.test.TestClass</mainClass>  

                      </manifest>  

                    </archive>  

                </configuration>  

                <executions>  

                  <execution>  

                    <id>make-assembly</id>  

                    <phase>package</phase>   

                    <goals>  

                        <goal>single</goal>  

                    </goals>  

                  </execution>  

                </executions>  

            </plugin>  

        </plugins>  

    </build>  

  

</project>  

 修改完pom后,在eclipse中右键项目run as 选择Maven package,可看打包的target目录内容:
 两个jar文件比较:



 

 

 参见:http://www.mkyong.com/maven/create-a-fat-jar-file-maven-assembly-plugin/

方法三、使用onejar-maven-plugin插件打可执行的jar包

测试类与方法一中一样,只是pom不一样,pom文件如下:

Java代码 

<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”>  

  <modelVersion>4.0.0</modelVersion>  

  <groupId>com.lwf.MavenPackage</groupId>  

  <artifactId>MavenPackage</artifactId>  

  <version>0.0.1-SNAPSHOT</version>  

  <name>MavenPackage</name>  

  <dependencies>  

    <dependency>  

      <groupId>log4j</groupId>  

      <artifactId>log4j</artifactId>  

      <version>1.2.17</version>  

    </dependency>  

    <dependency>  

      <groupId>fbcds</groupId>  

      <artifactId>fbcds</artifactId>  

      <version>1.0</version>  

    </dependency>  

    <dependency>   

      <groupId>ojdbc7</groupId>  

      <artifactId>ojdbc7</artifactId>  

      <version>1.0</version>   

    </dependency>  

  </dependencies>  

    <build>  

        <plugins>              

            <!– 使用 onejar-maven-plugin插件打可执行包–>  

            <plugin>  

                <groupId>org.apache.maven.plugins</groupId>  

                <artifactId>maven-jar-plugin</artifactId>  

                <configuration>  

                    <archive>  

                        <manifest>  

                            <mainClass>com.lwf.test.TestClass</mainClass>  

                        </manifest>  

                    </archive>  

                </configuration>  

            </plugin>  

            <plugin>  

                <groupId>com.jolira</groupId>  

                <artifactId>onejar-maven-plugin</artifactId>  

                <version>1.4.4</version>  

                <executions>  

                    <execution>  

                        <configuration>  

                            <attachToBuild>true</attachToBuild>  

                            <classifier>onejar</classifier>  

                        </configuration>  

                        <goals>  

                            <goal>one-jar</goal>  

                        </goals>  

                    </execution>  

                </executions>  

            </plugin>  

        </plugins>  

    </build>  

</project>  

打包截图如下:



 
 

参见:http://www.mkyong.com/maven/maven-create-a-fat-jar-file-one-jar-example/

上文中因googlecode中已没有onejar-maven-plugin所以另请参见下文:

http://my.oschina.net/noahxiao/blog/78241

方法四:使用maven-jar-plugin和maven-dependency-plugin打可执行包,引用的包放包外面文件夹下

其他不变,pom文件如下

Java代码 

<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”>  

  <modelVersion>4.0.0</modelVersion>  

  <groupId>com.lwf.MavenPackage</groupId>  

  <artifactId>MavenPackage</artifactId>  

  <version>0.0.1-SNAPSHOT</version>  

  <name>MavenPackage</name>  

  <dependencies>  

    <dependency>  

      <groupId>log4j</groupId>  

      <artifactId>log4j</artifactId>  

      <version>1.2.17</version>  

    </dependency>  

    <dependency>  

      <groupId>fbcds</groupId>  

      <artifactId>fbcds</artifactId>  

      <version>1.0</version>  

    </dependency>  

    <dependency>   

      <groupId>ojdbc7</groupId>  

      <artifactId>ojdbc7</artifactId>  

      <version>1.0</version>   

    </dependency>  

  </dependencies>  

    <build>  

        <plugins>  

 

<!– 方法四:使用maven-jar-plugin和maven-dependency-plugin打可执行包,引用的包放包外面文件夹下 –>  

            <plugin>  

                <groupId>org.apache.maven.plugins</groupId>  

                <artifactId>maven-jar-plugin</artifactId>  

                <configuration>  

                  <excludes>  

                    <exclude>**/log4j.properties</exclude>  

                  </excludes>  

                  <archive>  

                    <manifest>  

                    <addClasspath>true</addClasspath>  

                    <mainClass>com.lwf.test.TestClass</mainClass>  

                    <classpathPrefix>lib/</classpathPrefix>  

                    </manifest>  

                  </archive>  

                </configuration>  

            </plugin>  

  

            <!– Copy project dependency –>  

            <plugin>  

                <groupId>org.apache.maven.plugins</groupId>  

                <artifactId>maven-dependency-plugin</artifactId>  

                <version>2.5.1</version>  

                <executions>  

                  <execution>  

                    <id>copy-dependencies</id>  

                    <phase>package</phase>  

                    <goals>  

                        <goal>copy-dependencies</goal>  

                    </goals>  

                    <configuration>  

                      <!– exclude junit, we need runtime dependency only –>  

                      <includeScope>runtime</includeScope>  

                      <outputDirectory>${project.build.directory}/lib/</outputDirectory>  

                    </configuration>  

                  </execution>  

                </executions>  

            </plugin>              

        </plugins>  

    </build>  

</project>  

可以看到依赖的包拷贝到了lib目录下,打的包里没有依赖包的信息,只是简单的包,不过Manifest文件class-path要包含引用名的路径

Java代码 

 

Manifest-Version: 1.0  

Built-By: lweifeng  

Build-Jdk: 1.7.0_17  

Class-Path: lib/log4j-1.2.17.jar lib/fbcds-1.0.jar lib/ojdbc7-1.0.jar  

Created-By: Apache Maven 3.3.9  

Main-Class: com.lwf.test.TestClass  

Archiver-Version: Plexus Archiver  

 
 在以上前三种插件打包方式中,maven-shade-plugin和maven-assembly-plugin采取的是将依赖包解压再一并打到新包中,这样依赖包可能存在冲突的时候,导致运行时可能出现未知问题,而onejar-maven-plugin打包是将依赖包自动归入lib目录,不解压原包,相当于在原包基础上加壳,这样可以避免冲突的发生。第四种方法即是我们原来ant打包所使用的方法。

对于war包系统来说,有时候需要把jar包打到相关的war包中,可以使用plugin,默认将lib下的所有jar文件打包到WEB-INF/lib下。当然也是可以打包其他的文件的,诸如xml,properties等的。相关的plugin如下:

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <warName>${project.artifactId}</warName>
                <webResources>
                    <resource>
                        <directory>lib/</directory>
                        <targetPath>WEB-INF/lib</targetPath>
                        <includes>
                            <include>**/*.jar</include>
                        </includes>
                    </resource>
                </webResources>
            </configuration>
        </plugin>

从git拉取代码 并进行 maven编译

#!/bin/bash
DATE=`date +%F-%H-%M-%S`
FORK=master
TAG=0
v=版本号
WPATH=/打包根路径

#build fork-1.2.0
if [ $# = 1 ]
then
  echo "打包分支:$1"
  FORK=$1
fi

#build tag 1.2.0.release
if [ $# = 2 ]
then
  echo "打包TAG:$2"
  FORK=$1
  TAG=$2
fi

mkdir -p $WPATH/wars
#delete maven cache
rm -rf /root/.m2/repository/项目包路径


cd $WPATH
#init git
if [ ! -d "$WPATH/代码根目录" ] || [ ! -f "$WPATH/代码根目录/pom.xml" ];
then
    echo "git clone XXX项目"
    git clone http://代码路径 >/dev/null 2>&1
fi

#打包项目
cd $WPATH/代码根目录
echo "mvn install XXX项目"
git fetch origin
#build branch
if [ $TAG = 0 ]
then
  git checkout -b $FORK origin/$FORK
  git checkout $FORK
  git pull origin $FORK >/dev/null 2>&1
fi
#build tag
if [ $TAG != 0 ]
then
  git tag -l | xargs git tag -d
  git fetch -t -p -f
  git checkout $TAG
fi
#git -c core.quotepath=false checkout commitID^0 --
mvn clean install -Dmaven.test.skip=true >$WPATH/pack.log

test=`grep "BUILD SUCCESS" $WPATH/pack.log`
if [ ! "$test" = "" ];
then
\cp target/*-$v.jar $WPATH/wars/
echo "mvn OK"
else
  echo "mvn Fail"
fi

maven自动打包命令(windows环境)

echo off

set version=1.0.0
set env=simulation
set base_path=E:\project\demo
set dest_path=E:\deploy\demo

echo 基础包install 开始

SET lenInstall=3

SET pkgLib[0]=lib1
SET pkgLib[1]=lib2
SET pkgLib[2]=lib3

SET idx=0

:LoopLib
IF %idx% EQU %lenInstall% GOTO StartSrv

FOR /F "usebackq delims==. tokens=1-2" %%I IN (`SET pkgLib[%idx%]`) DO (
cd /d %base_path%\%%J
cmd /k "mvn clean install -Dmaven.test.skip=true&&exit"
echo %%J finished
)

SET /A idx=%idx% + 1

GOTO LoopLib

:StartSrv
echo 基础包install 完成


echo 服务包package 开始

SET lenPkg=3

SET pkgSrv[0]=service1
SET pkgSrv[1]=service2
SET pkgSrv[2]=service3

SET idx=0

:LoopSrv
IF %idx% EQU %lenPkg% GOTO Finished

FOR /F "usebackq delims==. tokens=1-2" %%I IN (`SET pkgSrv[%idx%]`) DO (
cd /d %base_path%\%%J
cmd /k "mvn clean package -Dmaven.test.skip=true -P%env%&&exit"
cd target
cp *%version%.* %dest_path%\
ECHO %%J finished
)

SET /A idx=%idx% + 1

GOTO LoopSrv

:Finished
echo 服务包package 完成

 

echo on

pause

Maven和Findbugs集成

1.配置pom.xml

<build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>findbugs-maven-plugin</artifactId>
                <version>3.0.4</version>
                <configuration>
                    <!--配置文件-->
                    <configLocation>${basedir}/findbugs-config.xml</configLocation>
                    <!--排除的BUG规则-->
                    <excludeFilterFile>custom-findbugs-exclude.xml</excludeFilterFile>
                    <!--包含的BUG规则-->
                    <includeFilterFile>custom-findbugs-include.xml</includeFilterFile>
                    <!--检查等级-->
                    <threshold>High</threshold>
                    <effort>Default</effort>
                    <!-- 输出配置 -->
                    <findbugsXmlWithMessages>true</findbugsXmlWithMessages>
                    <findbugsXmlOutput>true</findbugsXmlOutput>
                    <xmlOutput>true</xmlOutput>
                    <findbugsXmlOutputDirectory>target/site</findbugsXmlOutputDirectory>
                </configuration>
            </plugin>
        </plugins>
    </build>

custom-findbugs-exclude.xml内容:

<FindBugsFilter>
    <Match>
        <Bug pattern="DLS_DEAD_LOCAL_STORE"/>
    </Match>

    <Match>
        <Bug pattern="DM_BOXED_PRIMITIVE_FOR_PARSING"/>
    </Match>

    <Match>
        <Bug pattern="EI_EXPOSE_REP"/>
    </Match>

    <Match>
        <Bug pattern="EI_EXPOSE_REP2"/>
    </Match>

    <Match>
        <Bug pattern="ME_ENUM_FIELD_SETTER"/>
    </Match>
</FindBugsFilter>

2. 运行findbugs任务(先运行“mvn package”编译工程 )

mvn findbugs:help      
查看findbugs插件的帮助  
mvn findbugs:check      
检查代码是否通过findbugs检查,如果没有通过检查,检查会失败,但检查不会生成结果报表  
mvn findbugs:findbugs   
检查代码是否通过findbugs检查,如果没有通过检查,检查不会失败,会生成结果报表保存在target/findbugsXml.xml文件中  
mvn findbugs:gui        
检查代码并启动gui界面来查看结果  

具体fndbugs插件的配置项可以参考http://mojo.codehaus.org/findbugs-maven-plugin/findbugs-mojo.html