简体   繁体   English

如何在 Maven POM 中处理 3rd 方 JAR 和远程存储库?

[英]How to handle 3rd party JARs and remote repository in Maven POM?

I'm using a Maven dependency that requires setting up a remote repository.我正在使用需要设置远程存储库的 Maven 依赖项。 In the same project, I'm using a custom built JAR and trying to add it as a dependency as well.在同一个项目中,我使用自定义构建的 JAR 并尝试将其添加为依赖项。 The problem is that I get errors saying that Maven cannot find my custom JAR in the remote repository.问题是我收到错误消息,说 Maven 在远程存储库中找不到我的自定义 JAR。

In my POM I have multiple dependencies, including my custom built dependency and the dependency requiring the remote repository (confluent).在我的 POM 中,我有多个依赖项,包括我的自定义构建依赖项和需要远程存储库(合流)的依赖项。 I have tried putting my custom dependency first in the POM and that did not help.我尝试将我的自定义依赖项放在 POM 中,但没有帮助。 I tried removing the repository from the POM and I don't get the error about my custom built dependency, but I get an error for the remote one.我尝试从 POM 中删除存储库,但没有收到关于我的自定义构建依赖项的错误,但我收到了远程依赖项的错误。

I'm running the code in a Maven Docker container.我在 Maven Docker 容器中运行代码。 I've tried running the Docker container with a Bash shell and without the Maven commands, then manually ran the Maven commands inside the container, and manually checked the ~/.m2/repository and confirmed that my custom built JAR is in there.我已经尝试使用 Bash shell 运行 Docker 容器并且没有 Maven 命令,然后在容器内手动运行 Maven 命令,并手动检查~/.m2/repository并确认我的自定义构建的 JAR 在那里。

Ran an interactive Maven container:运行一个交互式 Maven 容器:

docker run -it --rm --name ProcessedObsGen -v "$(pwd)":/usr/src/mymaven \
  -w /usr/src/mymaven maven:3.3-jdk-8 /bin/bash

Inside the Docker container:在 Docker 容器内部:

mvn clean install:install-file \
  -Dfile=/usr/src/mymaven/libs/daas-utilities-0.0.1-SNAPSHOT.jar \
  -DgroupId=atlas -DartifactId=daas-utilities -Dversion=0.0.1-SNAPSHOT -Dpackaging=jar
mvn exec:java -Dexec.mainClass="atlas.processed_obs_generator.App"
ls ~/.m2/repository

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>atlas</groupId>
    <artifactId>processed-obs-generator</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>processed-obs-generator</name>
    <url>http://maven.apache.org</url>

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

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.kafka</groupId>
            <artifactId>kafka-clients</artifactId>
            <version>0.10.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.avro</groupId>
            <artifactId>avro</artifactId>
            <version>1.9.0</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.25</version>
        </dependency>
        <dependency>
            <groupId>atlas.daas-utilities</groupId>
            <artifactId>daas-utilities</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>io.confluent</groupId>
            <artifactId>kafka-avro-serializer</artifactId>
            <version>5.0.0</version>
        </dependency>
    </dependencies>
    <repositories>
        <repository>
            <id>confluent</id>
            <url>https://packages.confluent.io/maven/</url>
        </repository>
    </repositories>
</project>

I run the code as a Docker container with the command:我使用以下命令将代码作为 Docker 容器运行:

docker run -it --rm --name ProcessedObsGen -v "$(pwd)":/usr/src/mymaven \
  -w /usr/src/mymaven maven:3.3-jdk-8 mvn clean install:install-file \
  -Dfile=/usr/src/mymaven/libs/daas-utilities-0.0.1-SNAPSHOT.jar \
  -DgroupId=atlas -DartifactId=daas-utilities -Dversion=0.0.1-SNAPSHOT -Dpackaging=jar; \
  mvn exec:java -Dexec.mainClass="atlas.processed_obs_generator.App"

I get the error:我收到错误:

Failed to execute goal on project processed-obs-generator: Could not resolve dependencies for project atlas:processed-obs-generator:jar:0.0.1-SNAPSHOT: Could not find artifact atlas.daas-utilities:daas-utilities:jar:0.0.1-SNAPSHOT in confluent ( https://packages.confluent.io/maven/ )无法在项目处理的 obs-generator 上执行目标:无法解决项目 atlas:processed-obs-generator:jar:0.0.1-SNAPSHOT: 找不到工件 atlas.daas-utilities:daas-utilities:jar: 的依赖项: 0.0.1-SNAPSHOT 融合 ( https://packages.confluent.io/maven/ )

I've also tried downloading the confluent jar, putting it in the same location that I'm putting my custom jar, installing it the same way, and removing the remote repository from my pom.我还尝试下载融合的 jar,将它放在我放置自定义 jar 的相同位置,以相同的方式安装它,并从我的 pom.xml 中删除远程存储库。 Then I get errors with the confluent classes I'm using saying ClassNotFoundException.然后我在使用 ClassNotFoundException 的融合类中遇到错误。

I was able to solve it.我能够解决它。

I rebuilt my utilities jar with dependencies included.我重建了包含依赖项的实用程序 jar。 Please see Including dependencies in a jar with Maven .请参阅使用 Maven 在 jar 中包含依赖项

I kept my pom pretty much the same, although I removed some dependencies not being used.尽管我删除了一些未使用的依赖项,但我的 pom 几乎保持不变。

<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>atlas</groupId>
            <artifactId>daas-utilities</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>io.confluent</groupId>
            <artifactId>kafka-avro-serializer</artifactId>
            <version>5.0.0</version>
        </dependency>
    </dependencies>
    <repositories>
        <repository>
            <id>confluent</id>
            <url>https://packages.confluent.io/maven/</url>
        </repository>
    </repositories>

I placed my custom built jar in ${project.basedir}/libs.我将自定义构建的 jar 放在 ${project.basedir}/libs 中。

I adjusted the maven commands in my docker run command to include my jar with dependencies.我在我的 docker run 命令中调整了 maven 命令以包含我的 jar 和依赖项。 I also figured out that the mvn install:install-file ... is only installing the jar and is not (as I assumed) also installing your project.我还发现mvn install:install-file ...只是安装 jar 而不是(如我所假设的)也在安装您的项目。 So, I added another mvn clean install command at the end before I executed it.所以,我在执行之前添加了另一个mvn clean install命令。

docker run -it --rm --name ProcessedObsGen -v "$(pwd)":/usr/src/mymaven -w /usr/src/mymaven maven:3.3-jdk-8 mvn clean install:install-file -Dfile=/usr/src/mymaven/libs/daas-utilities-0.0.1-SNAPSHOT-jar-with-dependencies.jar -DgroupId=atlas -DartifactId=daas-utilities -Dversion=0.0.1-SNAPSHOT -Dpackaging=jar; mvn clean install; mvn exec:java -Dexec.mainClass="atlas.processed_obs_generator.App"

In your install-file commands you define -DgroupId=atlas while your POM reads:在您的install-file命令中,您在 POM 读取时定义-DgroupId=atlas

       ...
       <dependency>
            <groupId>atlas.daas-utilities</groupId>
            ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM