码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2020-04-01 14:48:33      阅读:73      评论:0      收藏:0      [点我收藏+]

标签:style   项目   tail   apache   desc   efs   target   odi   false   

注意,如果是依赖了本地公用项目,需要将本地公用项目通过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打包将依赖和配置外置的配置方式

标签:style   项目   tail   apache   desc   efs   target   odi   false   

原文地址:https://www.cnblogs.com/silentdoer/p/12612254.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!