您现在的位置:首页 >> 前端 >> 内容

一些常用的Maven Plugin配置

时间:2013/9/29 11:39:23 点击:

  核心提示:Maven是一个常用的Java build Manager, 使用Maven可以很好的对Java Project的dependency进行管理. 这里我记录几个比较常用的Plugin配置.生成JAR打...
Maven是一个常用的Java build Manager, 使用Maven可以很好的对Java Project的dependency进行管理. 这里我记录几个比较常用的Plugin配置. 

 

生成JAR打包文件:

[html] 

<plugin>  

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

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

                <configuration>  

                    <archive>  

                        <manifestEntries>  

                            <Class-Path>.</Class-Path>  

                        </manifestEntries>  

                    </archive>  

                    <excludes>  

                        <exclude>*.xml</exclude>  

                    </excludes>  

                </configuration>  

            </plugin>  

 

 

这个打包文件将不会把一般的XML配置文件也打包, 这样, 我们可以在不修改JAR文件的情况下对配置文件进行修改.

拷贝XML配置文件:

[html]  

<plugin>  

                <artifactId>maven-resources-plugin</artifactId>  

                <executions>  

                    <execution>  

                        <id>copy-resources</id>  

                        <phase>package</phase>  

                        <goals>  

                            <goal>copy-resources</goal>  

                        </goals>  

                        <configuration>  

                            <outputDirectory>target</outputDirectory>  

                            <resources>  

                                <resource>  

                                    <directory>src/main/resources</directory>  

                                    <filtering>true</filtering>  

                                </resource>  

                            </resources>  

                        </configuration>  

                    </execution>  

                </executions>  

            </plugin>  

这个plugin配置将把在src/main/resources文件夹下的配置文件都拷贝到target文件夹. 这种文件夹配置是IntelliJ下的默认配制.

拷贝所有的Dependency JARs:

[html]  

<plugin>  

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

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

                <executions>  

                    <execution>  

                        <id>copy-dependencies</id>  

                        <phase>package</phase>  

                        <goals>  

                            <goal>copy-dependencies</goal>  

                        </goals>  

                        <configuration>  

                            <outputDirectory>target/libs</outputDirectory>  

                            <overWriteReleases>false</overWriteReleases>  

                            <overWriteSnapshots>false</overWriteSnapshots>  

                            <overWriteIfNewer>true</overWriteIfNewer>  

                        </configuration>  

                    </execution>  

                </executions>  

            </plugin>  

这个plugin将拷贝所有的Dependency JARs到target/libs文件夹下, 这样只要添加这个目录到classpath下, 我们就可以在没有IDE的环境的情况下直接运行我们的程序. 

生成运行文件:

[html]  

<plugin>  

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

                <artifactId>maven-antrun-plugin</artifactId>  

                <executions>  

                    <execution>  

                        <id>manifest</id>  

                        <phase>package</phase>  

                        <goals>  

                            <goal>run</goal>  

                        </goals>  

                        <configuration>  

                            <tasks>  

                                <echo file="target/runjetty.sh">  

                                    java -cp "libs/*:jettysetup-1.0-SNAPSHOT.jar" weblog.examples.jettysetup.JettyLauncher  

                                </echo>  

                            </tasks>  

                        </configuration>  

                    </execution>  

                </executions>  

            </plugin>  

通过这个plugin配置, 我们将在target文件夹下生成一个叫runjetty.sh的文件来设置我们的classpath, 这样我们就可以通过这个脚本直接运行我们的程序了. 上面的这个例子是我在另一篇配置Jetty环境的文章中使用的, 整个的pom.xml配置文件可以在Github上找到.

通过上面的4个简单的配置, 我们可以实现对我们的项目进行打包, 同时拷贝配置文件, 和Dependency JAR文件, 生成执行脚本, 这个打包有几个优势:

 

打包文件不包含配置文件, 这样我们可以很方便得对配置文件进行修改

打包文件不包含Dependency JARs, 由于这些JAR并没有打包在我们的project jar文件里, 当我们有多个项目打包文件的时候, 这些JAR将会被共享.

当然, Maven并不是我们可以用的唯一一个build management tool, 我们还可以使用Ant, Ivy或者最近很流行的Gradle.

作者:网络 来源:不详