美文网首页
maven-shade-plugin jar 修改类引用路径

maven-shade-plugin jar 修改类引用路径

作者: 王小杰at2019 | 来源:发表于2019-02-12 13:00 被阅读9次

    http://maven.apache.org/plugins/maven-shade-plugin/

    需求1

    如果我们要提供一个 SDK 给被人用, 而我们自己的代码还依赖的别的 sdk 或者别的的 jar 包,这个时候在给别人用的时候就会出现 jar 包冲突的问题,或者隐藏着不兼容的问题, 那么这个时候我们就可以将依赖的包改名打包到一个 jar 包里面供他人引用

    代码演示

    1. maven配置

     <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>3.2.1</version>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                            <configuration>
                                <relocations>
                                    <relocation>
                                        <pattern>org.apache.commons.lang3</pattern>
                                        <shadedPattern>cn.wyj.commons.lang3</shadedPattern>
                                    </relocation>
                                </relocations>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
    
    

    2. 打包前的代码

    package cn.wyj.learn.maven;
    
    import lombok.extern.slf4j.Slf4j;
    import org.apache.commons.lang3.StringUtils;
    
    @Slf4j
    public class TestMerge {
        public static void main(String[] args) {
            String str = "hello world";
            boolean empty = StringUtils.isEmpty(str);
            log.info("empty: {}", empty);
            log.info("str: {}", str);
    
        }
    }
    
    

    3. 打包后的代码,反编译

    image.png
    
    package cn.wyj.learn.maven;
    
    import cn.wyj.commons.lang3.StringUtils;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    public class TestMerge {
        private static final Logger log = LoggerFactory.getLogger(TestMerge.class);
    
        public TestMerge() {
        }
    
        public static void main(String[] args) {
            String str = "hello world";
            boolean empty = StringUtils.isEmpty(str);
            log.info("empty: {}", empty);
            log.info("str: {}", str);
        }
    }
    
    

    相关文章

      网友评论

          本文标题:maven-shade-plugin jar 修改类引用路径

          本文链接:https://www.haomeiwen.com/subject/yooheqtx.html