关于maven打包配置整理,以及镜像配置、指定java版本

三种打包插件

maven-jar-plugin
Maven基础java打包插件,适合打api包或者纯手撕的代码包,就是没依赖第三方的,一般之前老项目Tomcat之类的分离打包都是这样用的,增量替换业务jar包部署就好了
maven-assembly-plugin
会将所有依赖的三方jar包的class打到项目的新jar包中,单个jar包就能跑了,默认会覆盖同名class文件
maven-shade-plugin(推荐)

会将所有依赖的三方jar包的class打到项目的新jar包中,单个jar包就能跑了,可配置处理追加同名class文件

1.maven-jar-plugin

替换到<project><build><plugins>标签下就好了,可执行jar包与依赖包是分开的,需要同目录建立lib目录来存放所需的依赖包,部署时注意lib包位置就好了,配置如下:

        <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-jar-plugin</artifactId>
                 <version>2.6</version>
                 <configuration>
                    <archive>
                         <manifest>
                             <addClasspath>true</addClasspath>
                             <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>com.xxx.xxxService</mainClass>
                       </manifest>
                    </archive>
                </configuration>
            </plugin>
            <!--不需要依赖lib包可以直接注释,脚本复制的lib的jar包-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.10</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                         <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

2.maven-assembly-plugin

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>com.xxx.xxxService</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <!--不需要依赖lib包可以直接注释,脚本复制的-->
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

3.maven-shade-plugin

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.1</version>
                <configuration>
                    <!--对于有多个第三方依赖的情况,第三方包 META-INF下会有一些相同的MINIFEST文件,maven-shade-plugin默认会使用追加而不是覆盖的方式处理它们。由于一些包重复引用,打包后的 META-INF 目录多出了一些 *.SF 等文件,执行jar包时,可能会抛出java.lang.SecurityException: Invalid signature file digest for Manifest main attributes异常。可以通过配置解决-->
                    <filters>
                        <filter>
                            <artifact>*:*</artifact>
                            <excludes>
                                <exclude>META-INF/*.SF</exclude>
                                <exclude>META-INF/*.DSA</exclude>
                                <exclude>META-INF/*.RSA</exclude>
                            </excludes>
                        </filter>
                    </filters>
                    <!--不在根目录生成打包时的缩减后的pom的xml临时文件-->
                    <createDependencyReducedPom>false</createDependencyReducedPom>
                    <transformers>
                        <!--对这个同名文件进行追加处理-->
                        <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                            <resource>reference.conf</resource>
                        </transformer>
                        <!--设置主函数-->
                        <transformer
                                implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                            <mainClass>cn.fht.test.test</mainClass>
                        </transformer>
                    </transformers>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

4.指定maven使用的java版本

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>

5.补个仓库镜像和插件镜像

这个要在<project>标签下

 <repositories>
        <repository>
            <id>aliyun-repos</id>
            <url>https://maven.aliyun.com/nexus/content/groups/public/</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>aliyun-plugin</id>
            <url>https://maven.aliyun.com/nexus/content/groups/public/</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值