Search code examples
mavennexusself-hosting

trying to push java repo to maven, it keeps giving me 401-UnAuthorized


I'm extracting all the common models from my project, and trying to move them into a separate repository:

<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.mua.common</groupId>
  <artifactId>models</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>models</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.22</version>
    </dependency>
  </dependencies>
  <distributionManagement>
    <repository>
      <id>models</id>
      <url>https://nexus.myhost.bd/repository/maven-central/</url>
    </repository>
    <snapshotRepository>
      <id>models-snapshots</id>
      <url>https://nexus.myhost.bd/repository/maven-central/</url>
    </snapshotRepository>
  </distributionManagement>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-deploy-plugin</artifactId>
        <version>3.0.0-M1</version>
        <executions>
          <execution>
            <id>deploy-artifacts</id>
            <phase>deploy</phase>
            <goals>
              <goal>deploy</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

  <repositories>
    <repository>
      <id>alt-models</id>
      <url>https://nexus.myhost.bd/repository/maven-central/</url>
    </repository>
  </repositories>

</project>

And in my C:\Users\<username>\.m2 folder I've added the settings.xml as well:

<settings>
  <servers>
    <server>
      <id>alt-models</id>
      <username>username</username>
      <password>password</password>
    </server>
  </servers>
</settings>

But whenever I click on deploy from intellij it gives me 401 - UnAuthorized exception.

I'm hosting a nexus to store my packages.

How can I resolve this? What am I doing wrong here?

Update (modified the settings.xml like this):

<?xml version="1.0" encoding="UTF-8"?>
<settings>


  <servers>
    <server>
      <id>models</id>
      <username>username</username>
      <password>password</password>
    </server>
    <server>
      <id>models-snapshot</id>
      <username>username</username>
      <password>password</password>
    </server>


  </servers>
</settings>

But it's still not working

Here is the complete log of failure:

C:\Users\User\.jdks\corretto-17.0.9\bin\java.exe -Dmaven.multiModuleProjectDirectory=K:\repos\models\models -Djansi.passthrough=true "-Dmaven.home=C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2023.2.3\plugins\maven\lib\maven3" "-Dclassworlds.conf=C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2023.2.3\plugins\maven\lib\maven3\bin\m2.conf" "-Dmaven.ext.class.path=C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2023.2.3\plugins\maven\lib\maven-event-listener.jar" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2023.2.3\lib\idea_rt.jar=51830:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2023.2.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2023.2.3\plugins\maven\lib\maven3\boot\plexus-classworlds-2.7.0.jar;C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2023.2.3\plugins\maven\lib\maven3\boot\plexus-classworlds.license" org.codehaus.classworlds.Launcher -Didea.version=2023.2.5 deploy
[INFO] Scanning for projects...
[INFO] 
[INFO] ---------------------< com.mua.common:models >----------------------
[INFO] Building models 1.0-SNAPSHOT
[INFO]   from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- resources:3.3.0:resources (default-resources) @ models ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory K:\repos\models\models\src\main\resources
[INFO] 
[INFO] --- compiler:3.10.1:compile (default-compile) @ models ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- resources:3.3.0:testResources (default-testResources) @ models ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory K:\repos\models\models\src\test\resources
[INFO] 
[INFO] --- compiler:3.10.1:testCompile (default-testCompile) @ models ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- surefire:3.0.0:test (default-test) @ models ---
[INFO] Using auto detected provider org.apache.maven.surefire.junit.JUnit3Provider
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.mua.common.AppTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.01 s - in com.mua.common.AppTest
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] 
[INFO] --- jar:3.3.0:jar (default-jar) @ models ---
[INFO] 
[INFO] --- install:3.1.0:install (default-install) @ models ---
[INFO] Installing K:\repos\models\models\pom.xml to C:\Users\User\.m2\repository\com\mua\common\models\1.0-SNAPSHOT\models-1.0-SNAPSHOT.pom
[INFO] Installing K:\repos\models\models\target\models-1.0-SNAPSHOT.jar to C:\Users\User\.m2\repository\com\mua\common\models\1.0-SNAPSHOT\models-1.0-SNAPSHOT.jar
[INFO] 
[INFO] --- deploy:3.0.0-M1:deploy (default-deploy) @ models ---
Downloading from models-snapshots: https://nexus.myhost.bd/repository/maven-central/com/mua/common/models/1.0-SNAPSHOT/maven-metadata.xml
[WARNING] Could not transfer metadata com.mua.common:models:1.0-SNAPSHOT/maven-metadata.xml from/to models-snapshots (https://nexus.myhost.bd/repository/maven-central/): status code: 401, reason phrase: Unauthorized (401)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  6.281 s
[INFO] Finished at: 2024-02-12T16:27:42+06:00
[INFO] ------------------------------------------------------------------------
[WARNING] 
[WARNING] Plugin validation issues were detected in 3 plugin(s)
[WARNING] 
[WARNING]  * org.apache.maven.plugins:maven-compiler-plugin:3.10.1
[WARNING]  * org.apache.maven.plugins:maven-deploy-plugin:3.0.0-M1
[WARNING]  * org.apache.maven.plugins:maven-resources-plugin:3.3.0
[WARNING] 
[WARNING] For more or less details, use 'maven.plugin.validation' property with one of the values (case insensitive): [BRIEF, DEFAULT, VERBOSE]
[WARNING] 
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:3.0.0-M1:deploy (default-deploy) on project models: ArtifactDeployerException: Failed to retrieve remote metadata com.mua.common:models:1.0-SNAPSHOT/maven-metadata.xml: Could not transfer metadata com.mua.common:models:1.0-SNAPSHOT/maven-metadata.xml from/to models-snapshots (https://nexus.myhost.bd/repository/maven-central/): status code: 401, reason phrase: Unauthorized (401) -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

Process finished with exit code 1

maven version:

Apache Maven 3.9.2 (c9616018c7a021c1c39be70fb2843d6f5f9b8a1c)

Solution

  • I had to add this under my module's pom.xml under project:

    <distributionManagement>
        <repository>
          <id>models</id>
          <url>https://nexus.myhost.bd/repository/mygroup/</url>
        </repository>
        <snapshotRepository>
          <id>models</id>
          <url>https://nexus.myhost.bd/repository/mygroup/</url>
        </snapshotRepository>
      </distributionManagement>
    
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-deploy-plugin</artifactId>
            <version>3.1.1</version>
            <executions>
              <execution>
                <id>deploy-artifacts</id>
                <phase>deploy</phase>
                <goals>
                  <goal>deploy</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    
      <repositories>
        <repository>
          <id>models</id>
          <url>https://nexus.myhost.bd/repository/mygroup/</url>
        </repository>
      </repositories>
    

    And in .settings.xml under ~/m2 (on linux), or in C:\Users\<username>\.m2, I have added this:

    <?xml version="1.0" encoding="UTF-8"?>
    <settings>
      <servers>
        <server>
          <id>models</id>
          <username>username</username>
          <password>password</password>
        </server>
      </servers>
    </settings>
    

    And finally, in the project where I want to use I module, I had to add these lines under project:

    
        <repositories>
            <repository>
                <id>models</id>
                <url>https://nexus.myhost.bd/repository/mygroup/</url>
            </repository>
        </repositories>
    

    Then we can add our dependency in dependencies section, and build it without any error.