1.记一次maven引入本地依赖

1.背景

客户环境并没有某些三方依赖,无法实现docker化部署

1.1 方案一

将那几个依赖的本地仓库,复制到docker打包的机器,让package时从本地仓库引入
可行,之前临时解决方案,缺点就是,客户调整服务器,需要再次将本地仓库复制到对应服务器上

1.2 本地引入

// java可以通过<systemPath>${basedir}/lib/combined-lib.jar</systemPath>

        <dependency>
            <groupId>tech.ty</groupId>
            <artifactId>pdi</artifactId>
            <version>1.0</version>
            <scope>system</scope>
            <systemPath>${basedir}/lib/combined-lib.jar</systemPath>
        </dependency>

将包放置项目的lib目录下,但是打包并没有将对应的lib/*.jar 打包到 BOOT-INF/lib 下面,需要增加plugin

  <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.2.0</version>
                <executions>
                    <execution>
                        <id>copy-local-jar-to-target</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/classes/BOOT-INF/lib/</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${project.basedir}/lib</directory>
                                    <includes>
                                        <include>*.jar</include>
                                    </includes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

2.整合成一个依赖

由于我引入的第三方包是一个pom,内部间接引入了其他依赖

        <dependency>
            <groupId>pentaho-kettle</groupId>
            <artifactId>kettle-core</artifactId>
            <version>${kettle.version}</version>
        </dependency>
        <dependency>
            <groupId>pentaho-kettle</groupId>
            <artifactId>kettle-engine</artifactId>
            <version>${kettle.version}</version>
        </dependency>

2.1 shade

通过shade可以将所有lib打包成一个jar

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-parent</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>

        <dependency>
            <groupId>pentaho-kettle</groupId>
            <artifactId>kettle-core</artifactId>
            <version>${kettle.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.poi</groupId>
                    <artifactId>poi</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.apache.poi</groupId>
                    <artifactId>poi-ooxml</artifactId>
                </exclusion>
                <exclusion>
                <groupId>org.codehaus.groovy</groupId>
                <artifactId>groovy-all</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>pentaho-kettle</groupId>
            <artifactId>kettle-engine</artifactId>
            <version>${kettle.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.poi</groupId>
                    <artifactId>poi</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.apache.poi</groupId>
                    <artifactId>poi-ooxml</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.codehaus.groovy</groupId>
                    <artifactId>groovy-all</artifactId>
                </exclusion>
                <exclusion>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-core</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>javax.servlet</groupId>
                    <artifactId>servlet-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.4</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <filters>
                                <filter>
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SF</exclude>
                                        <exclude>META-INF/*.DSA</exclude>
                                        <exclude>META-INF/*.RSA</exclude>
                                    </excludes>
                                </filter>
                            </filters>
                            <shadedArtifactAttached>false</shadedArtifactAttached>
                            <shadedArtifactClassifier>shaded</shadedArtifactClassifier>
                            <outputFile>${project.build.directory}/combined-lib.jar</outputFile>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

我之所以要排除exclusion,通过maven依赖存在远近先后顺序问题,
还有版本冲突,不同包会出现目录项目 类名项目,这个就需要自己控制依赖顺序问题
否则报错classnotfound
还有就是我们目前这两个依赖打包的成jar过大,100多M,当前的springboot版本启动会报错,及需要分析内部包,先排除,然后再从本项目自己引入,
自此,项目运行正常,中间会遇到很多问题,需要自己细心依依排查,程序员不易~

3.gradle扩展

gradle项目更简单,可以通过批量导入,maven找了很多博客并未发现,本项目那两个内部依赖100+,显然一个个引入不现实,建议其他的小伙伴,慢慢从maven转成gradle,gradle功能更强大

implementation fileTree('dir': 'lib', include: ['*.jar'])
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容