让IntelliJ在多模块maven项目中导入着色依赖项

时间:2015-05-22 23:02:21

标签: maven intellij-idea

我有两个模块,组件和应用程序。由于构建过程中稍后的依赖冲突(谷歌协议缓冲区),组件模块被着色。

<!-- snip from Component's 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> 

应用程序取决于组件模块。但是,Application中的源文件无法引用Component所依赖的着色库。这对于与Component进行交互至关重要。

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

即使IntelliJ找不到导入,Maven构建也能正常工作。我错过了什么/做错了什么?

5 个答案:

答案 0 :(得分:7)

太糟糕了,在使用JetBrains打开票证之后,此问题先前已报告过,目前在IntelliJ中不受支持。

答案 1 :(得分:3)

我通过着色源jar并将其解压缩到源目录来解决类似的问题:

<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>

答案 2 :(得分:3)

build-helper-maven-plugin似乎已将阴影罐添加到IntelliJ项目中:

  <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>

有关详细信息,请参见https://www.mojohaus.org/build-helper-maven-plugin/attach-artifact-mojo.html

答案 3 :(得分:0)

我在当前项目中使用的另一种解决方案。

对于着色模块或自定义包,其工作方式相同。

<button id="save0" class="lol">Click me</button>

答案 4 :(得分:-1)

更好的解决方法似乎是:在IntelliJ的项目视图中,右键单击碎片项目“组件”-> pom.xml,选择“ Maven”->“忽略项目”。然后在项目pom.xml上执行“ Maven”->“重新导入”。