To shade or not to shade

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: 你的项目需要使用Joda2.1,但是Elasticsearch 2.0.0-beta2 要求使用Joda2.8 2.0.0-beta2 org.

你的项目需要使用Joda2.1,但是Elasticsearch 2.0.0-beta2 要求使用Joda2.8

<properties>
    <elasticsearch.version>2.0.0-beta2</elasticsearch.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch</artifactId>
        <version>${elasticsearch.version}</version>
    </dependency>
    <dependency>
        <groupId>org.elasticsearch.plugin</groupId>
        <artifactId>shield</artifactId>
        <version>${elasticsearch.version}</version>
    </dependency>
    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>2.1</version>
    </dependency>
</dependencies>
<properties>
    <elasticsearch.version>2.0.0-beta2</elasticsearch.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch</artifactId>
        <version>${elasticsearch.version}</version>
    </dependency>
    <dependency>
        <groupId>org.elasticsearch.plugin</groupId>
        <artifactId>shield</artifactId>
        <version>${elasticsearch.version}</version>
    </dependency>
    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>2.1</version>
    </dependency>
</dependencies>

遮罩ES

新建一个maven工程,pom.xml如下:

<groupId>my.elasticsearch.test</groupId>
<artifactId>es-shaded</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
    <elasticsearch.version>2.0.0-beta2</elasticsearch.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch</artifactId>
        <version>${elasticsearch.version}</version>
    </dependency>
    <dependency>
        <groupId>org.elasticsearch.plugin</groupId>
        <artifactId>shield</artifactId>
        <version>${elasticsearch.version}</version>
    </dependency>
</dependencies>
<repositories>
    <repository>
        <id>elasticsearch-releases</id>
        http://maven.elasticsearch.org/releases
        <releases>
            <enabled>true</enabled>
            <updatePolicy>daily</updatePolicy>
        </releases>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>
<groupId>my.elasticsearch.test</groupId>
<artifactId>es-shaded</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
    <elasticsearch.version>2.0.0-beta2</elasticsearch.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch</artifactId>
        <version>${elasticsearch.version}</version>
    </dependency>
    <dependency>
        <groupId>org.elasticsearch.plugin</groupId>
        <artifactId>shield</artifactId>
        <version>${elasticsearch.version}</version>
    </dependency>
</dependencies>
<repositories>
    <repository>
        <id>elasticsearch-releases</id>
        http://maven.elasticsearch.org/releases
        <releases>
            <enabled>true</enabled>
            <updatePolicy>daily</updatePolicy>
        </releases>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

遮罩并且重定位冲突的包

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.4.1</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <relocations>
                            <relocation>
                                <pattern>org.joda</pattern>
                                <shadedPattern>my.elasticsearch.joda</shadedPattern>
                            </relocation>
                        </relocations>
                        <transformers>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer" />
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.4.1</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <relocations>
                            <relocation>
                                <pattern>org.joda</pattern>
                                <shadedPattern>my.elasticsearch.joda</shadedPattern>
                            </relocation>
                        </relocations>
                        <transformers>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer" />
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

运行 mvn clean install 将会创建遮罩后的Elasticsearch版本。Joda2.8也被遮罩起来了

现在你可以同时使用Elasticsearch 2.0.0-beta2和Joda2.1了

<!-- This artifact contains all elasticsearch libs including the shaded version of Joda and Shield -->
<dependency>
    <groupId>my.elasticsearch.test</groupId>
    <artifactId>es-shaded</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency> <!-- You still use your own Joda version -->
<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.1</version>
</dependency>

使用你自己的版本的Joda,只需要import org.joda.time.DateTime
使用遮罩后版本的Joda,importing my.elasticsearch.joda.time.DateTime,不过一般不建议这样做
因为debug的时候跟源码对不上?

// import org.joda.time.DateTime;
// import my.elasticsearch.joda.time.DateTime;
CodeSource codeSource = new org.joda.time.DateTime().getClass().getProtectionDomain().getCodeSource();
System.out.println("unshaded = " + codeSource);
codeSource = new my.elasticsearch.joda.time.DateTime().getClass().getProtectionDomain().getCodeSource();
System.out.println("shaded = " + codeSource);
相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
目录
相关文章
|
4月前
|
Java Maven Spring
SpringBoot 系列之 Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resource
这篇文章描述了在使用Maven构建Spring Boot项目时遇到的`maven-resources-plugin`插件版本问题导致的编译失败,并提供了通过修改插件版本至3.1.0来解决这个问题的方法。
SpringBoot 系列之 Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resource
|
5月前
|
Java Maven
Cannot resolve plugin org.apache.maven.plugins:maven-site-plugin:3.3
Cannot resolve plugin org.apache.maven.plugins:maven-site-plugin:3.3
|
Java Maven
Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4
Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4
|
7月前
|
移动开发 监控 前端开发
常见plugin
常见plugin
77 0
|
Java Maven Android开发
成功解决maven打war包报错:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2
成功解决maven打war包报错:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2
|
Java
JavaFX使用maven-shade-plugin打成Jar包
JavaFX使用maven-shade-plugin打成Jar包
121 0
JavaFX使用maven-shade-plugin打成Jar包
|
Java Maven
org.apache.maven.plugins:maven-resources-plugin:2.4.3
在myeclipse上创建maven可能会遇到 org.apache.maven.plugins:maven-resources-plugin:2.4.3 org.apache.maven.plugins:maven-jar-plugin:2.3.1 org.apache.maven.maven-surefire-plugin:2.7.1 等错误那么这是什么原因呢?
155 0
org.apache.maven.plugins:maven-resources-plugin:2.4.3
|
Java Maven
Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.10.1:compile (default-compil
maven-compliler-plugin版本与maven版本不一致,Maven版本太低或maven-compiler-plugin版本过高
3879 0
Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.10.1:compile (default-compil
Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.1:test
Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.1:test
132 0
Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.1:test
|
Java Maven Windows
解决 Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile
解决 Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:compile
2474 0