论坛首页 Java企业应用论坛

【飞天奔月出品】使用Maven打all-in-one的包(带tests 和 sources)(多方案实现对比)

浏览 4164 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2016-12-20  

使用Maven打all-in-one的包(带tests 和 resources)(多方案实现对比)

1.痛点

feilong-spring 项目子项目很多

使用 Maven依赖的话,要写很多代码

<project>

    ....
    <properties>
        <version.feilong-platform>1.9.6</version.feilong-platform>
        ....
    </properties>

    ....
    <repositories>
        <repository>
            <id>feilong-repository</id>
            <url>https://raw.github.com/venusdrogon/feilong-platform/repository</url>
        </repository>
    </repositories>

    ....
    <dependencies>
        ....
        <dependency>
            <groupId>com.feilong.platform.spring</groupId>
            <artifactId>feilong-spring-core</artifactId>
            <version>${version.feilong-platform}</version>
        </dependency>
        <dependency>
            <groupId>com.feilong.platform.spring</groupId>
            <artifactId>feilong-spring-aop</artifactId>
            <version>${version.feilong-platform}</version>
        </dependency>
        <dependency>
            <groupId>com.feilong.platform.spring</groupId>
            <artifactId>feilong-spring-context</artifactId>
            <version>${version.feilong-platform}</version>
        </dependency>
        <dependency>
            <groupId>com.feilong.platform.spring</groupId>
            <artifactId>feilong-spring-jdbc</artifactId>
            <version>${version.feilong-platform}</version>
        </dependency>
        <dependency>
            <groupId>com.feilong.platform.spring</groupId>
            <artifactId>feilong-spring-web</artifactId>
            <version>${version.feilong-platform}</version>
        </dependency>
        ....
    </dependencies>

    ....

</project>

能否做个 all-in-one 的jar(such as quartz-all,netty-all) ,只需要依赖这一个jar 即可?

2.解决方案

2.1 方案1:将所有的项目合并成一个项目

调整项目结构,类似于 feilong taglib issues1 ,但是 feilong-spring 不适合 ,文件太杂,维护起来很困难

image

2.2 方案2:使用 maven-antrun-plugin 插件

在我百思不得其解的时候,想起"咦,别人是怎么做的?",此时 github 开源的力量是无穷的,我查阅了quartz-all

他使用了 maven-antrun-plugin 插件, 我借鉴并修改成以下一版 pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.feilong.platform.spring</groupId>
        <artifactId>parent</artifactId>
        <version>1.10.0-SNAPSHOT</version>
    </parent>

    <artifactId>feilong-spring-all</artifactId>
    <packaging>jar</packaging>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <id>packing-all</id>
                        <phase>process-sources</phase>
                        <configuration>
                            <tasks>
                                <copy todir="${project.build.directory}/classes">
                                    <fileset dir="${basedir}/../feilong-spring-aop/target/classes" />
                                    <fileset dir="${basedir}/../feilong-spring-context/target/classes" />
                                    <fileset dir="${basedir}/../feilong-spring-core/target/classes" />
                                    <fileset dir="${basedir}/../feilong-spring-jdbc/target/classes" />
                                    <fileset dir="${basedir}/../feilong-spring-mobile/target/classes" />
                                    <fileset dir="${basedir}/../feilong-spring-web/target/classes" />
                                </copy>
                            </tasks>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

2.2.1 依赖结果

随便找个项目,依赖下测试

<dependencies>
    ...
    <dependency>
        <groupId>com.feilong.platform.spring</groupId>
        <artifactId>feilong-spring-all</artifactId>
        <version>1.10.0-SNAPSHOT</version>
    </dependency>
    ...
</dependencies>

显示:

image

2.2.2 缺点:

没有source 以及 test 包

image

不利于查看javadoc 以及学习

2.3 方案3: 使用 maven-assembly-plugin插件

那么我们继续寻找其他方案, 咦,发现 shiro-all 在仓库中有 pom.xml

image

他是怎么做到的呢?

查看下他的源码 , 可以看到他使用了 maven-assembly-plugin 插件

pom.xml

<plugins>
    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
            <execution>
                <id>make-bundles</id>
                <goals>
                    <goal>single</goal>
                </goals>
                <phase>package</phase>
                <!---->
                <configuration>
                    <descriptors>
                        <descriptor>src/main/assembly/assembly.xml</descriptor>
                    </descriptors>

                    <!-- We _do_ want the assembly output to be the actual artifact produced for this Maven module. -->
                    <!-- not append assembly id in release file name -->
                    <appendAssemblyId>false</appendAssemblyId>
                </configuration>
            </execution>
        </executions>
    </plugin>
</plugins>

assembly.xml

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">

    <id>jar</id>

    <formats>
        <format>jar</format>
    </formats>

    <includeBaseDirectory>false</includeBaseDirectory>

    <dependencySets>
        <dependencySet>
            <outputDirectory>/</outputDirectory>
            <useProjectArtifact>false</useProjectArtifact>
            <unpack>true</unpack>
            <useTransitiveDependencies>false</useTransitiveDependencies>
        </dependencySet>
    </dependencySets>

</assembly>

2.3.1 缺点:

依赖 feilong-spring-all jar, 项目还会自动依赖 feilong-spring-all 所依赖的jar

image

image

shiro-all 1.4.0-RC2 写法也一样不行

image

重复依赖很明显

2.4 方案4: 使用 maven-shade-plugin 插件

shiro-all 1.3.2 支持 sources image

并且依赖之后,没有明显的重复依赖 :thumbsup: (This is what i want what i really really want)

image

shiro-all 1.3.2 系列使用的maven-shade-plugin 插件

image

2.4.1 feilong-spring-all 配置

feilong-spring-all pom.xml 配置如下

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.feilong.platform.spring</groupId>
        <artifactId>parent</artifactId>
        <version>1.10.0-SNAPSHOT</version>
    </parent>

    <artifactId>feilong-spring-all</artifactId>
    <packaging>jar</packaging>

    <properties>
        <v.maven-shade-plugin>2.4.3</v.maven-shade-plugin>
    </properties>

    <dependencies>

        <dependency>
            <groupId>com.feilong.platform.spring</groupId>
            <artifactId>feilong-spring-aop</artifactId>
            <version>${project.version}</version>
        </dependency>

        <dependency>
            <groupId>com.feilong.platform.spring</groupId>
            <artifactId>feilong-spring-context</artifactId>
            <version>${project.version}</version>
        </dependency>

        <dependency>
            <groupId>com.feilong.platform.spring</groupId>
            <artifactId>feilong-spring-core</artifactId>
            <version>${project.version}</version>
        </dependency>

        <dependency>
            <groupId>com.feilong.platform.spring</groupId>
            <artifactId>feilong-spring-jdbc</artifactId>
            <version>${project.version}</version>
        </dependency>

        <dependency>
            <groupId>com.feilong.platform.spring</groupId>
            <artifactId>feilong-spring-mobile</artifactId>
            <version>${project.version}</version>
        </dependency>

        <dependency>
            <groupId>com.feilong.platform.spring</groupId>
            <artifactId>feilong-spring-web</artifactId>
            <version>${project.version}</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>${v.maven-shade-plugin}</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <!-- With version 2.2 of the maven-shade-plugin, they added a "shadeTestJar" option (see MSHADE-158): http://maven.apache.org/plugins/maven-shade-plugin/shade-mojo.html#shadeTestJar -->
                            <shadeTestJar>true</shadeTestJar>
                            <!-- When true, it will attempt to create a sources jar as well -->
                            <createSourcesJar>true</createSourcesJar>

                            <artifactSet>
                                <includes>
                                    <include>${project.groupId}:*:*</include>
                                </includes>
                            </artifactSet>

                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

配置方式中增加了两个属性参数:

2.4.2 feilong-spring-all deploy之后的情况

参见 https://github.com/venusdrogon/feilong-platform/tree/repository/com/feilong/platform/spring/feilong-spring-all/1.10.0-SNAPSHOT

image

可以看到 pom.xml ,jar,sources jar,tests jar 一应齐全,棒棒哒 :clap:

2.4.3 feilong-spring-all 被依赖情况

很完美,做到了只依赖feilong-spring-all,并且不再依赖 feilong-spring-all 自身依赖的jar

搞定,收工~~

3.参考

----完

论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics