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

阅读更多

1.痛点

feilong-spring 项目子项目很多

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

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

<project>

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

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

    ....
    <dependencies>
        ....
        <dependency>
            <groupId>com.feilong.platform.springgroupId>
            <artifactId>feilong-spring-coreartifactId>
            <version>${version.feilong-platform}version>
        dependency>
        <dependency>
            <groupId>com.feilong.platform.springgroupId>
            <artifactId>feilong-spring-aopartifactId>
            <version>${version.feilong-platform}version>
        dependency>
        <dependency>
            <groupId>com.feilong.platform.springgroupId>
            <artifactId>feilong-spring-contextartifactId>
            <version>${version.feilong-platform}version>
        dependency>
        <dependency>
            <groupId>com.feilong.platform.springgroupId>
            <artifactId>feilong-spring-jdbcartifactId>
            <version>${version.feilong-platform}version>
        dependency>
        <dependency>
            <groupId>com.feilong.platform.springgroupId>
            <artifactId>feilong-spring-webartifactId>
            <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 不适合 ,文件太杂,维护起来很困难

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

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.0modelVersion>
    <parent>
        <groupId>com.feilong.platform.springgroupId>
        <artifactId>parentartifactId>
        <version>1.10.0-SNAPSHOTversion>
    parent>

    <artifactId>feilong-spring-allartifactId>
    <packaging>jarpackaging>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-antrun-pluginartifactId>
                <executions>
                    <execution>
                        <id>packing-allid>
                        <phase>process-sourcesphase>
                        <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>rungoal>
                        goals>
                    execution>
                executions>
            plugin>
        plugins>
    build>
project>

2.2.1 依赖结果

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

<dependencies>
    ...
    <dependency>
        <groupId>com.feilong.platform.springgroupId>
        <artifactId>feilong-spring-allartifactId>
        <version>1.10.0-SNAPSHOTversion>
    dependency>
    ...
dependencies>

显示:

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

2.2.2 缺点:

没有source 以及 test 包

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

不利于查看javadoc 以及学习

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

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

image

他是怎么做到的呢?

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

pom.xml

<plugins>
    <plugin>
        <artifactId>maven-assembly-pluginartifactId>
        <executions>
            <execution>
                <id>make-bundlesid>
                <goals>
                    <goal>singlegoal>
                goals>
                <phase>packagephase>
                
                <configuration>
                    <descriptors>
                        <descriptor>src/main/assembly/assembly.xmldescriptor>
                    descriptors>

                    
                    
                    <appendAssemblyId>falseappendAssemblyId>
                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>jarid>

    <formats>
        <format>jarformat>
    formats>

    <includeBaseDirectory>falseincludeBaseDirectory>

    <dependencySets>
        <dependencySet>
            <outputDirectory>/outputDirectory>
            <useProjectArtifact>falseuseProjectArtifact>
            <unpack>trueunpack>
            <useTransitiveDependencies>falseuseTransitiveDependencies>
        dependencySet>
    dependencySets>

assembly>

2.3.1 缺点:

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

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

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

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

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

重复依赖很明显

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

shiro-all 1.3.2 支持 sources 【飞天奔月出品】使用Maven打all-in-one的包(带tests 和 sources)(多方案实现对比)_第8张图片

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

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

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

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

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.0modelVersion>

    <parent>
        <groupId>com.feilong.platform.springgroupId>
        <artifactId>parentartifactId>
        <version>1.10.0-SNAPSHOTversion>
    parent>

    <artifactId>feilong-spring-allartifactId>
    <packaging>jarpackaging>

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

    <dependencies>

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

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

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

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

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

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

    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-shade-pluginartifactId>
                <version>${v.maven-shade-plugin}version>
                <executions>
                    <execution>
                        <phase>packagephase>
                        <goals>
                            <goal>shadegoal>
                        goals>
                        <configuration>
                            
                            <shadeTestJar>trueshadeTestJar>
                            
                            <createSourcesJar>truecreateSourcesJar>

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

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

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

  • shadeTestJar
  • createSourcesJar When true, it will attempt to create a sources jar as well

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

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

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

2.4.3 feilong-spring-all 被依赖情况

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

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

搞定,收工~~

3.参考

  • maven-assembly-plugin 插件
  • maven-antrun-plugin 插件
  • maven-shade-plugin 插件
  • http://stackoverflow.com/questions/5149130/how-can-i-configure-the-maven-shade-plugin-to-include-test-code-in-my-jar

----完

你可能感兴趣的:(maven)