Maven (eclipse) 使用阴影 jar 而不是原始 jar

Maven (eclipse) use shaded jar instead of original jar

我有一个项目,其中的包将被重新定位(阴影 jar)

pom.xml:

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

  <artifactId>artifact-child</artifactId>
  <name>artifact-child</name>

  <parent>
    <groupId>group</groupId>
    <artifactId>artifact</artifactId>
    <version>${revision}</version>
  </parent>

  <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.2.2</version>

        <configuration>
          <!--  <shadedArtifactAttached>true</shadedArtifactAttached> -->

          <relocations>
            <reloaction>
              <pattern>example</pattern>
              <shadedPattern>${super-package}.example</shadedPattern>
            </reloaction>
          </relocations>          
        </configuration>

        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
          </execution>
        </executions>
      </plugin>  
    </plugins>
  </build>
</project>

我还有第二个项目,在 运行 时需要阴影罐。

pom.xml:

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

  <groupId>testversion</groupId>
  <artifactId>testv</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>testv</name>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>group</groupId>
      <artifactId>artifact-child</artifactId>
      <version>0.0.1</version>
      <classifier>shaded</classifier>
    </dependency>
  </dependencies>
</project>

我的问题:

Eclipse在他的workspace中找到另一个项目,直接使用源码

例如:我必须写import example.myclass而不是import org.example.myclass

在某些情况下,这可能是个问题。能不能说,maven或者eclipse应该用"shaded jar"代替原来的源码?

我必须创建一个在线存储库,以便 maven 只能下载阴影 jar 吗?



我找到了另外两个 Whosebug 帖子(没有结果):

Maven Eclipse multi-module shaded dependency

How to install shaded jar instead of the original jar

已解决:
我的错误:
1. 父版本必须直接声明而不是通过属性
2.忘记运行"Maven Install"

解决方案:
Maven 运行 没有错误,但 Eclipse 使用打开的项目而不是阴影 jar。
在这里找到解决方案:How can I download a single raw file from a private github repo using the command line? .
打开项目的属性。在 Tap Maven 下,取消勾选 "Resolve dependencies from Workspace projects"

如果阴影 jar 是您的依赖项,您只需引用 classes/packages,因为它们在阴影 jar 中定义。

如果 Eclipse 显示错误,则 Eclipse 出错(这种情况经常发生)。首先,尝试使用 clean verify 之类的东西构建您的项目。然后你看看你是否真的有错误(或者如果 Eclipse 编造了错误)。

如果 Eclipse 错误困扰您,请尝试 ALT+F5。