java - Maven (eclipse) 使用阴影 jar 代替原始 jar

标签 java eclipse maven dependencies uberjar

我有一个项目,其中的包将被重新定位(阴影 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>

我有第二个项目,它需要在运行时使用阴影 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>

  <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在他的工作区中找到另一个项目并直接使用源代码。

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

在某些情况下,这可能是一个问题。是否可以说,什么maven或eclipse应该使用“shaded jar”而不是原始源代码?

我必须创建一个在线存储库,这样maven只能下载shaded jar吗?



我发现了另外两篇 stackoverflow 帖子(没有结果):

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? .
打开项目的属性。在点击 Maven 下,删除“解决工作空间项目的依赖关系”的勾选

最佳答案

如果阴影 jar 是您的依赖项,您只需引用阴影 jar 中定义的类/包即可。

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

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

关于java - Maven (eclipse) 使用阴影 jar 代替原始 jar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60510702/

相关文章:

java - 是否可以通过在 run() 方法中返回来安全地停止 Java 线程?

java - org.joda.time.DateTime.parse 在 Netbans 中没问题,但在 eclipse 中出错

java - mvn 依赖 :tree doesn't really show all dependencies

java - 调试 jaxb2 maven 插件

java - 如何将postgres脚本传输到hsqldb脚本?

java - 隐藏先前显示的数组元素

android - 即使没有错误,也无法在 Eclipse 上启动 Android 模拟器

spring - 使用 Maven/JUnit/Spring 在第一次失败时停止测试

java - 是否有任何 Java 库可以对 http.conf 等 unix 配置文件进行操作

eclipse - 如何清除 Eclipse 中的验证错误?