简体   繁体   English

为什么我的 maven 项目无法解析私有 sonatype-nexus maven 存储库的依赖项?

[英]Why my maven project is not able to resolve dependency from private sonatype-nexus maven repository?

I have deployed a private maven repository on an aws ec2 instance.我已经在 aws ec2 实例上部署了一个私有 maven 存储库。 And I am able to deploy few artefacts successfully as shown.如图所示,我能够成功部署一些人工制品。

Maven 仓库

Maven 仓库

My settings.xml looks like following我的 settings.xml 如下所示

<settings>
  <mirrors>
    <mirror>
      <!--This sends everything else to /public -->
      <id>nexus</id>
      <mirrorOf>*</mirrorOf>
      <url>http://maven.tarkshala.com:10081/repository/maven-tarkshala-central/</url>
    </mirror>
  </mirrors>
  <profiles>
    <profile>
      <id>nexus</id>
      <!--Enable snapshots for the built in central repo to direct -->
      <!--all requests to nexus via the mirror -->
      <repositories>
        <repository>
          <id>central</id>
          <url>http://central</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </repository>
      </repositories>
     <pluginRepositories>
        <pluginRepository>
          <id>central</id>
          <url>http://central</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </pluginRepository>
      </pluginRepositories>
    </profile>
  </profiles>
<activeProfiles>
    <!--make the profile active all the time -->
    <activeProfile>nexus</activeProfile>
  </activeProfiles>
  <servers>
    <server>
      <id>nexus</id>
      <username>admin</username>
      <password>tarkshala</password> 
    </server>
  </servers>
</settings>

My project pom file looks like this我的项目 pom 文件看起来像这样

<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.tarkshala.dhaba</groupId>
    <artifactId>user-service</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>UserService</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <dropwizard.version>2.1.4</dropwizard.version>
        <dropwizard.guicey.version>5.4.1</dropwizard.guicey.version>
        <dropwizard.swagger.version>2.0.16-SNAPSHOT</dropwizard.swagger.version>
        <lombok.version>1.18.24</lombok.version>
        <db.sharding.bundle.version>2.0.28-5</db.sharding.bundle.version>
        <mariadb.version>3.1.0</mariadb.version>

        <mainClass>com.tarkshala.dhaba.UserServiceApplication</mainClass>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>io.dropwizard</groupId>
                <artifactId>dropwizard-dependencies</artifactId>
                <version>${dropwizard.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>io.dropwizard</groupId>
            <artifactId>dropwizard-core</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
        </dependency>
        <dependency>
            <groupId>jakarta.validation</groupId>
            <artifactId>jakarta.validation-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate.validator</groupId>
            <artifactId>hibernate-validator</artifactId>
        </dependency>
        <dependency>
            <groupId>io.dropwizard</groupId>
            <artifactId>dropwizard-hibernate</artifactId>
        </dependency>
        <dependency>
            <groupId>io.dropwizard</groupId>
            <artifactId>dropwizard-db</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>javax.persistence-api</artifactId>
        </dependency>

        <dependency>
            <groupId>com.tarkshala</groupId>
            <artifactId>dropwizard-swagger</artifactId>
            <version>2.0.16-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
        </dependency>

        <dependency>
            <groupId>ru.vyarus</groupId>
            <artifactId>dropwizard-guicey</artifactId>
            <version>${dropwizard.guicey.version}</version>
        </dependency>

        <dependency>
            <groupId>io.appform.dropwizard.sharding</groupId>
            <artifactId>db-sharding-bundle</artifactId>
            <version>${db.sharding.bundle.version}</version>
        </dependency>

        <dependency>
            <groupId>org.mariadb.jdbc</groupId>
            <artifactId>mariadb-java-client</artifactId>
            <version>${mariadb.version}</version>
        </dependency>

        <dependency>
            <groupId>com.tarkshala</groupId>
            <artifactId>tarkshala-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>com.tarkshala.dhaba</groupId>
            <artifactId>userservice-models</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.4.0</version>
                <configuration>
                    <createDependencyReducedPom>true</createDependencyReducedPom>
                    <transformers>
                        <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                        <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                            <mainClass>${mainClass}</mainClass>
                        </transformer>
                    </transformers>
                    <!-- exclude signed Manifests -->
                    <filters>
                        <filter>
                            <artifact>*:*</artifact>
                            <excludes>
                                <exclude>META-INF/*.SF</exclude>
                                <exclude>META-INF/*.DSA</exclude>
                                <exclude>META-INF/*.RSA</exclude>
                            </excludes>
                        </filter>
                    </filters>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.3.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>${mainClass}</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.10.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
            </plugin>
            <plugin>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.2.1</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>3.4.1</version>
                <executions>
                    <execution>
                        <id>attach-javadocs</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>9</source><target>9</target></configuration></plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>9</source>
                    <target>9</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>11</source>
                    <target>11</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <reporting>
        <plugins>
            <plugin>
                <artifactId>maven-project-info-reports-plugin</artifactId>
                <version>3.4.1</version>
                <configuration>
                    <dependencyLocationsEnabled>false</dependencyLocationsEnabled>
                    <dependencyDetailsEnabled>false</dependencyDetailsEnabled>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>3.4.1</version>
            </plugin>
        </plugins>
    </reporting>
    <profiles>
        <profile>
            <id>java11+</id>
            <activation>
                <jdk>[11,)</jdk>
            </activation>
            <properties>
                <!--
                Workaround for "javadoc: error - The code being documented uses modules but the packages
                defined in https://docs.oracle.com/javase/8/docs/api/ are in the unnamed module."
                -->
                <maven.javadoc.skip>true</maven.javadoc.skip>
            </properties>
        </profile>
    </profiles>
</project>

But as I try to build it using mvn clean install it fails because of not able to resolve dependency with following error:但是当我尝试使用mvn clean install构建它时,它失败了,因为无法解决依赖关系并出现以下错误:

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.395 s
[INFO] Finished at: 2023-01-15T16:22:06+05:30
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project user-service: Could not resolve dependencies for project com.tarkshala.dhaba:user-service:jar:1.0-SNAPSHOT: The following artifacts could not be resolved: com.tarkshala:dropwizard-swagger:jar:2.0.16-SNAPSHOT, com.tarkshala:tarkshala-common:jar:1.0-SNAPSHOT: com.tarkshala:dropwizard-swagger:jar:2.0.16-SNAPSHOT was not found in http://maven.tarkshala.com:10081/repository/maven-tarkshala-central/ during a previous attempt. This failure was cached in the local repository and resolution is not reattempted until the update interval of nexus has elapsed or updates are forced -> [Help 1]

This is happening because my artefact is present in maven-snapshots not in maven-tarkshala-central .发生这种情况是因为我的人工制品存在于maven-snapshots而不是maven-tarkshala-central

What should I change to resolve dependencies from both?我应该更改什么以解决两者的依赖关系?

This failure was cached in the local repository and resolution is not reattempted until the update interval of nexus has elapsed or updates are forced

  • Go to your local maven reposistory ~/.mvn/ and remove the io.dropwizard folder. Go 到本地 maven 存储库~/.mvn/并删除io.dropwizard文件夹。
  • Then build with mvn clean install -U然后使用mvn clean install -U构建

I figured it out finally.我终于想通了。 Basically nexus repository system has three types as following.基本上,nexus 存储库系统有以下三种类型。

  1. proxy, used to make a middleman repo to cache maven central repository.代理,用于制作中间人仓库以缓存 maven 中央仓库。 In this example it is maven-tarkshala-central在这个例子中它是maven-tarkshala-central
  2. hosted, used to deploy our private artefacts.托管,用于部署我们的私有工件。 In this example, it is maven-snapshots and maven-releases .在此示例中,它是maven-snapshotsmaven-releases

My settings.xml was pointing to proxy, so it was able to resolve all external dependencies but not able to resolve private dependencies.我的 settings.xml 指向代理,因此它能够解析所有外部依赖项但无法解析私有依赖项。 If I make it point one of the hosted it resolves private but not central.如果我将它指向托管之一,它会解析为私有但不是中央。

  1. group, there is third type of repository which help to deal with the problem mentioned above.组,还有第三种类型的存储库,有助于处理上述问题。 It acts as parent repository and keep proxy and hosted repos as members.它充当父存储库,并将代理和托管存储库作为成员。 So all the dependencies available in all three maven-tarkshala-central, maven-snapshots, maven-releases get available into this repo as shown in screenshot.因此,所有三个maven-tarkshala-central, maven-snapshots, maven-releases可用的所有依赖项都可用于此 repo,如屏幕截图所示。

组回购配置

Check the members that are available in group, mind the sequence.检查组中可用的成员,注意顺序。

Update settings.xml to use the group repo url as following更新 settings.xml 以使用组 repo url,如下所示

<settings>
    <mirrors>
      <mirror>
        <!--This sends everything else to /public -->
        <id>nexus</id>
        <mirrorOf>*</mirrorOf>
        <url>http://maven.tarkshala.com:10081/repository/maven-public/</url>
      </mirror>
    </mirrors>
    <profiles>
      <profile>
        <id>nexus</id>
        <!--Enable snapshots for the built in central repo to direct -->
        <!--all requests to nexus via the mirror -->
        <repositories>
          <repository>
            <id>central</id>
            <url>http://central</url>
            <releases><enabled>true</enabled></releases>
            <snapshots><enabled>true</enabled></snapshots>
          </repository>
        </repositories>
       <pluginRepositories>
          <pluginRepository>
            <id>central</id>
            <url>http://central</url>
            <releases><enabled>true</enabled></releases>
            <snapshots><enabled>true</enabled></snapshots>
          </pluginRepository>
        </pluginRepositories>
      </profile>
    </profiles>
  <activeProfiles>
      <!--make the profile active all the time -->
      <activeProfile>nexus</activeProfile>
    </activeProfiles>
    <servers>
      <server>
        <id>nexus</id>
        <username>admin</username>
        <password>tarkshala</password> 
      </server>
    </servers>
  </settings>

And now happy building with mvn clean install :)现在很高兴使用mvn clean install构建 :)

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

相关问题 如何使用Nexus存储库解决Maven中的依赖性错误 - How to resolve dependency errors in Maven with Nexus repository Docker自动构建从Maven Sonatype Nexus存储库下载私有jar - Docker Automated build downloading private jars from a Maven Sonatype Nexus repository 使 Sonatype Nexus 存储库可公开访问,以便能够使用 URL 下载工件,例如 maven 中央 - Making Sonatype Nexus Repository publically accessible to be able to download artifacts with URL like maven central Sonatype Nexus:如何使用Seagate个人云存储设备作为Maven工件的存储库? - Sonatype Nexus: How to use my Seagate personal cloud storage device as a repository for my maven artifacts? 如何从远程存储库向我的Java项目添加Maven依赖关系? - How to add a Maven dependency from a remote repository to my Java project? Maven 无法从本地存储库(nexus)下载某些依赖项或插件 - Maven cannot Download from local repository (nexus) for some dependency or plugins Eclipse无法解决Maven依赖关系 - Eclipse not able to resolve Maven dependency 使用Nexus Repository OSS 3创建Maven项目[离线] - Create Maven Project with Nexus Repository OSS 3 [offline] 添加 GitLab 私有存储库作为 Maven 依赖项 - Add GitLab private repository as Maven dependency Gradle无法解决IntelliJ中的Sonatype Nexus依赖关系 - Gradle unable to resolve Sonatype Nexus dependency in IntelliJ
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM