Java-Maven(十):Maven 项目常用plugins

本文主要总结最近一段时间使用maven时,遇到需要maven plugins的一些简单总结。

1)在Build下重新指定最终打包报名

    <build>
        <!--最终打包的包名,如果这里不指定,则默认包名为:artifactId-version.jar(com-test-project-1.0-SNAPSHOT.jar)-->
        <finalName>The-Test-Pro</finalName>
    ...
    </build>

2)Maven Repository上以外的自定义包引入,以及如何设置才能被打包进来

a)在dependencies下引入lib下的jar

        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-core_2.11-1.5.2.logging</artifactId>
            <version>2.11</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/spark-core_2.11-1.5.2.logging-2.11.jar</systemPath>
        </dependency>

b)在maven-compiler-plugin配置包时将/lib下的包打包进来

            <!--代码编译,指定扩展包在lib下,且打包时将/lib下的包打包进来-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <source>1.8</source>
                    <target>1.8</target>
                    <showWarnings>true</showWarnings>
                    <compilerArguments>
                        <extdirs>${project.basedir}/lib</extdirs>
                    </compilerArguments>
                </configuration>
            </plugin>

3)通过maven-assembly-plugin设置编译fat包

            <!-- 
            在pom中配置了若干依赖,需要将pom中所有的依赖全部打包进一个jar包中,可以选择的方案有maven-assembly-plugin
            此时,在Target下会打包一个finalName-jar-with-dependencies.jar,(不指定finalName时:artifactId-version-with-dependencies.jar)
             -->
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>com.main.Invoker</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

4)使用maven-dependency-plugin拷贝依赖的jar包到/target/lib目录

            <!-- 拷贝依赖的jar包到/target/lib目录 -->  
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>
                                ${project.build.directory}/lib
                            </outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

 

posted @ 2019-06-14 09:00  cctext  阅读(7809)  评论(0编辑  收藏  举报