• 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、设置字符集

    1
    2
    3
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

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

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

    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    <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、编译代码

    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    <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文件

    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    <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、构建测试用例配置

    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    <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文件到指定目录

    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    <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

    1
    2
    3
    4
    5
    6
    <repositories>
        <repository>
            <id>cloudera</id>
        </repository>
    </repositories>

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

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

    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    <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>
  • 相关阅读:
    Flare3D游戏特效教程:火拳阿宝
    AS3:物体的运动
    Away3D基础教程(三):三维世界的灯光
    代码库工具:SVN
    磨刀不误砍柴工:取巧而已
    可视化组件:Swing JComponent
    软件简单升级方式:文件覆盖
    代码库工具:CVS
    多字符串查找算法:kmp与step
    格式化文本支持:JTextPane
  • 原文地址:https://www.cnblogs.com/zzwx/p/8820220.html
Copyright © 2020-2023  润新知