问题
I've been using Shade to relocate a dependency from com.package.x
to com.package.y
; when I build with Maven, it complains due to incompatible types - so I have to change my import statements inside my code to match com.package.y
.
Is this really the only way to go about this? Changing the imports is making IntelliJ complain and basically breaks IDE integration. Is there no way Shade can modify the imports?
回答1:
The main use case of the shade plugin is to generate an uber-jar while relocating some packages inside of it to prevent conflicts. As a rule you don't want to be developing against an uberjar.
If you actually do want to develop against your shaded jar (or you're solely using shade to relocate an existing jar), then you'll need to list your shaded jar as a in lieu of the original jar, which I would imagine may need you to clean up a cyclic dependency.
回答2:
This issue can be fixed by seting shadedArtifactAttached to true in configuration tag.
<shadedArtifactAttached>true</shadedArtifactAttached>
Complete example and Reference: https://maven.apache.org/plugins/maven-shade-plugin/examples/attached-artifact.html
I had the same issue and i fixed it by adding the above property. I am sharing my pom.xml of my shaded module just in case if you want to see any other tag values.
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>org.bitguiders.account.management</groupId>
<artifactId>account-management</artifactId>
<version>1.0.24-SNAPSHOT</version>
</parent>
<artifactId>account-management-metrics-lib</artifactId>
<name>account-management-metrics-lib</name>
<description>Bundled client library</description>
<properties>
<shaded.package>org.cas.osd.platform.ciam.shaded</shaded.package>
</properties>
<dependencies>
<dependency>
<groupId>org.cas.osd.platform.ciam.account.management</groupId>
<artifactId>account-management-metrics</artifactId>
<version>${project.parent.version}</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<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>
<shadedArtifactAttached>true</shadedArtifactAttached>
<createDependencyReducedPom>true</createDependencyReducedPom>
<promoteTransitiveDependencies>true</promoteTransitiveDependencies>
<createSourcesJar>true</createSourcesJar>
<relocations>
<!-- manually hide individual dependencies that a client might conflict with -->
<relocation>
<pattern>org.</pattern>
<shadedPattern>${shaded.package}.org.</shadedPattern>
<excludes>
<exclude>org.slf4j.**</exclude>
<exclude>org.xml.**</exclude>
<exclude>org.w3c.**</exclude>
<exclude>org.bitguiders.usagemetrics.**</exclude>
<exclude>org.bitguiders.**</exclude>
<exclude>org.bitguiders.**</exclude>
<exclude>org.apache.flume.**</exclude>
<exclude>org.apache.http.**</exclude>
</excludes>
</relocation>
<relocation>
<pattern>com.</pattern>
<shadedPattern>${shaded.package}.com.</shadedPattern>
<excludes>
<exclude>com.google.common.**</exclude>
<exclude>com.sun.xml.bind.xmlDeclaration</exclude>
</excludes>
</relocation>
<relocation>
<pattern>javassist</pattern>
<shadedPattern>${shaded.package}.javassist</shadedPattern>
</relocation>
<relocation>
<pattern>javax.ws.rs</pattern>
<shadedPattern>${shaded.package}.javax.ws.rs</shadedPattern>
</relocation>
<relocation>
<pattern>jersey.repackaged</pattern>
<shadedPattern>${shaded.package}.jersey.repackaged</shadedPattern>
</relocation>
<relocation>
<pattern>net.</pattern>
<shadedPattern>${shaded.package}.net.</shadedPattern>
</relocation>
</relocations>
<artifactSet>
<!-- these external dependencies should be exposed to clients -->
<excludes>
<exclude>org.slf4j:slf4j-api</exclude>
<exclude>org.bitguiders.cpp:cpp-product-usage-ingest-gateway-client</exclude>
</excludes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
来源:https://stackoverflow.com/questions/29807418/refactoring-import-statements-using-shade-relocations-in-maven