maven打包将依赖和配置外置的配置方式

Posted Silentdoer

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了maven打包将依赖和配置外置的配置方式相关的知识,希望对你有一定的参考价值。

注意,如果是依赖了本地公用项目,需要将本地公用项目通过mvn install安装到本地maven仓库;

将项目依赖的jar包外置到主jar包所在目录的lib目录,resources文件外置到主jar包所在目录的配置:

<build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.6</version>

                <configuration>
                    <archive>
                        <!-- 配置打包后jar包内的元数据,比如classpath是什么【从哪里找依赖】 -->
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>me.silentdoer.demomybladetest.Entrance</mainClass>
                        </manifest>
                        <manifestEntries>
                            <Class-Path>/</Class-Path>
                        </manifestEntries>
                    </archive>
                    <excludes>
                        <exclude>
                            <!-- 排除文件的写法 -->
                            <exclude>**/*.xml</exclude>
                            <!-- 排除resources内的某个目录打包到项目jar包内 -->
                            <exclude>/config/**</exclude>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.7</version>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.maven.shared</groupId>
                        <artifactId>maven-filtering</artifactId>
                        <version>1.3</version>
                    </dependency>
                </dependencies>

                <executions>
                    <execution>
                        <!-- 如果不行这个换成copy-config试试 -->
                        <id>copy-resources</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <!-- 将resources的内容复制到target目录里 -->
                            <outputDirectory>${project.build.directory}/</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>src/main/resources</directory>
                                </resource>
                            </resources>
                            <encoding>UTF-8</encoding>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.8</version>
                <executions>
                    <!-- 这里是否可以增加多个execution和通过include等来实现将部分依赖直接压缩到主jar包内? -->
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <!-- 将依赖复制到target目录下的lib目录里 -->
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <!-- 要包含间接依赖 -->
                            <excludeTransitive>false</excludeTransitive>
                            <!-- 不忽略版本 -->
                            <stripVersion>false</stripVersion>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.18.1</version>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>

            <!-- 可以用来将所有依赖都打进主jar包里,但好像和上面的插件不兼容 -->
            <!--<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>me.silentdoer.demomybladetest.Entrance</mainClass>
                        </manifest>
                    </archive>
                    <detail>true</detail>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            &lt;!&ndash; 2.6版本好像还可以assembly &ndash;&gt;
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>-->
        </plugins>
    </build>

 

以上是关于maven打包将依赖和配置外置的配置方式的主要内容,如果未能解决你的问题,请参考以下文章

彩蛋推荐 |SpringBoot技术专题「Tomcat技术专区」用正确的姿势如何用外置tomcat配置及运行(Tomcat优化分析)

SpringBoot中如何使用外置tomcat服务器

idea设置打包方式不包含依赖

如何用maven将配置文件打在jar包外

maven的两种可运行jar打包方式。1内置依赖,2外部依赖。

maven打包$env没有值