【问题标题】:Getting IntelliJ to import shaded dependencies in a multi-module maven project让 IntelliJ 在多模块 Maven 项目中导入阴影依赖项
【发布时间】:2015-05-22 23:02:21
【问题描述】:

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

<!-- 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 所依赖的着色库。这对于与组件交互至关重要。

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

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

【问题讨论】:

  • 我也遇到了同样的问题(与您描述的情况完全相同)。在其他 SO 帖子中,他们建议转到 File -> Invalidate Caches / Restart。我有时而不是有时看到这项工作(这一次,它不起作用)。例如,对于较早的分支,使缓存无效并重新启动有效,但对于另一个分支,这不起作用。我想知道如何解决这个问题。
  • 我最终将阴影模块分离到它自己的项目中。当然不是一个通用的解决方案,但由于 CI 的原因,它最终在我们的案例中工作得更好。但是,我将 Q 保持打开状态,希望有另一种解决方案。如果没有,我会用 JetBrains 开票。
  • 您有机会使用 JetBrains 开票吗?我仍然卡住了..不幸的是,我在一个大项目上,并且没有选择将 maven 模块重构到自己的项目中。
  • 这周我没有休假。如果更紧急,请随时打开。
  • 您使用的是哪个版本的 IntelliJ?

标签: maven intellij-idea


【解决方案1】:

太糟糕了,用 JetBrains 开票后,这个问题之前已经报告过,目前 IntelliJ 不支持。

【讨论】:

  • 在第一张票中,最后一条评论给出了一个对我有用的好解决方法:更好的解决方法似乎是:在项目视图中右键单击 shade-bug-repackaged -> pom.xml在 IntelliJ 中,选择“Maven”->“忽略项目”。然后在顶级 pom.xml 上执行“Maven”->“重新导入”。
【解决方案2】:

build-helper-maven-plugin 似乎将阴影 jar 添加到 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】:

    我通过对源 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>
    

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        【解决方案5】:

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

        它对着色模块或自定义包的工作方式相同。

        <!--
          - Make sure 'module-a' is built BEFORE this module
          -->
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>module-a</artifactId>
            <version>${project.version}</version>
            <scope>test</scope><!-- or runtime -->
        </dependency>
        
        <!--
          - IDEA does NOT support custom bundles in multi-module projects.
          - Using system scope to link correct JAR file explicitly.
         -->
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>module-a</artifactId>
            <version>FAKE-VERSION</version>
            <classifier>FAKE-CLASSIFIER</classifier>
            <scope>system</scope>
            <systemPath>${project.basedir}/../module-a/target/module-a-${project.version}.jar</systemPath>
        </dependency>
        

        【讨论】:

          【解决方案6】:

          作为对 TimP 回答的补充:

          • 通过使用build-helper-maven-plugin 附加附加工件,Idea 能够识别它并作为传递依赖项正常工作(依赖于工件的其他模块也可以识别重定位符号)。

          • 如果您不想打包额外的工件,只需在execution 配置中添加skipAttach(阴影工件仍被IDEA 识别):

          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <executions>
              <execution>
                     <id>attach-shade-jar</id>
                     <phase>package</phase>
                     <goals>
                        <goal>attach-artifact</goal>
                     </goals>
                     <configuration>
                         <artifacts>
                            <artifact>   
            <file>${project.build.directory}/${project.build.finalName}.jar</file>
                                <type>jar</type>
                                <classifier>shaded</classifier>
                            </artifact>
                         </artifacts>
                         <skipAttach>true</skipAttach>
                    </configuration>
                </execution>
             </executions>
          </plugin>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-02-18
            • 2015-08-31
            • 2012-07-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多