Getting IntelliJ to import shaded dependencies in a multi-module maven project

I have two modules: Component and Application. The Component module is obscured due to a dependency conflict (google protocol buffers) later in the build process.

<!-- snip from Component pom.xml --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.3</version> <configuration> <relocations> <relocation> <pattern>com.google.protobuf</pattern> <shadedPattern>my.package.protocols.shaded.com.google.protobuf</shadedPattern> </relocation> </relocations> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> </execution> </executions> </plugin> 

The application depends on the Component module. However, the source files in the application cannot reference the shaded library on which the component depends. This is important for interacting with a component.

  <-- snip from Application pom.xml --> <dependency> <groupId>my-group</groupId> <artifactId>component</artifactId> <version>${project.version}</version> </dependency> 

Although no import was found by IntelliJ, the Maven build works fine. What am I missing / doing wrong?

+13
intellij-idea maven
source share
4 answers

Too bad, after opening a ticket with JetBrains, this problem was previously reported and is currently not supported in IntelliJ.

+7
source share

I fixed a similar problem by painting over the source jar file as well and unpacking it into the source directory:

 <build> <sourceDirectory>${project.build.directory}/shaded-sources</sourceDirectory> <plugins> <plugin> <artifactId>maven-source-plugin</artifactId> <configuration> <skipSource>true</skipSource> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.1.0</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <createSourcesJar>true</createSourcesJar> <shadeSourcesContent>true</shadeSourcesContent> <createDependencyReducedPom>false</createDependencyReducedPom> <artifactSet> <includes> <include>com.google.guava:*</include> </includes> </artifactSet> <relocations> <relocation> <pattern>com.google.common</pattern> <shadedPattern>org.apache.drill.shaded.com.google.common</shadedPattern> </relocation> </relocations> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>unpack</id> <phase>package</phase> <goals> <goal>unpack</goal> </goals> <configuration> <artifactItems> <artifactItem> <groupId>org.apache.drill.contrib</groupId> <artifactId>guava-shaded</artifactId> <version>${project.version}</version> <type>jar</type> <outputDirectory>${project.build.directory}/classes</outputDirectory> <includes>**/**</includes> </artifactItem> <artifactItem> <groupId>org.apache.drill.contrib</groupId> <artifactId>guava-shaded</artifactId> <version>${project.version}</version> <type>jar</type> <classifier>sources</classifier> <overWrite>true</overWrite> <outputDirectory>${project.build.directory}/shaded-sources</outputDirectory> <includes>**/**</includes> </artifactItem> </artifactItems> </configuration> </execution> </executions> </plugin> </build> 
+3
source share

build-helper-maven-plugin seems to add a shaded jar to an IntelliJ project:

  <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <executions> <execution> <id>compile</id> <phase>package</phase> <goals> <goal>attach-artifact</goal> </goals> <configuration> <artifacts> <artifact> <file>${basedir}/target/<YOUR JAR NAME>-SNAPSHOT.jar</file> <type>jar</type> <classifier>optional</classifier> </artifact> </artifacts> </configuration> </execution> </executions> </plugin> 

For more information, see https://www.mojohaus.org/build-helper-maven-plugin/attach-artifact-mojo.html .

+3
source share

The best workaround seems to be: right-click on the "component" of the shard project β†’ pom.xml in the project view in IntelliJ, select "Maven" β†’ "Ignore projects". Then do "Maven" -> "Reimport" for the pom.xml project.

0
source share

All Articles