how to get maven archetypes from my own authenticated nexus without username and password in the URL?

StackOverflow https://stackoverflow.com/questions/14811535

Question

I have a private Nexus with a repository protected via authentication.

Pulling libraries works like a charm, but if I want to use one of the archetypes stored up there I always need to write plaintext username and password in the URL of the archetype catalog like this:

mvn archetype:generate -DarchetypeCatalog=http://username:password@maven.mycompany.com/nexus/content/repositories/myrepo/archetype-catalog.xml

I read http://maven.apache.org/archetype/maven-archetype-plugin/faq.html#authentication and updated my settings.xml with what I understood from that very tiny bit of help:

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
                      http://maven.apache.org/xsd/settings-1.0.0.xsd">

  <servers>
    <server>
      <id>myrepo</id>
      <username>username</username>
      <password>{HASHED_PASSWORD}</password>
    </server>
    <server>
      <id>pretty-archetype-unicorn-repo</id>
      <username>username</username>
      <password>{HASHED_PASSWORD}</password>
    </server>
  </servers>

  <profiles>
   <profile>
     <id>someid</id>
     <repositories>
       <repository>
         <id>myrepo</id>
         <name>My Repo</name>
         <url>http://maven.mycompany.com/nexus/content/repositories/myrepo/</url>
       </repository>
     </repositories>
   </profile>
  </profiles>

  <activeProfiles>
    <activeProfile>someid</activeProfile>
  </activeProfiles>

</settings>

Needless to say, it doesn't work and when I try:

mvn archetype:generate -DarchetypeCatalog=http://maven.mycompany.com/nexus/content/repositories/myrepo/archetype-catalog.xml

I get the same old:

[WARNING] Error reading archetype catalog http://maven.mycompany.com/nexus/content/repositories/myrepo/archetype-catalog.xml
org.apache.maven.wagon.authorization.AuthorizationException: Access denied to: http://maven.mycompany.com/nexus/content/repositories/myrepo/archetype-catalog.xml

Any hints, or better documentation with working example?

Was it helpful?

Solution

There's currently no way to do that if you don't specify at least -DarchetypeArtifactId. As per the official docs you linked:

The server id used to download the artifact is [archetypeArtifactId]-repo

hence there's no way to just browse the catalog if it's password protected (and you're not willing to expose username/password on your shell history).

In the meanwhile, you can go ahead and vote for ARCHETYPE-204. They have a patch already available since years, they probably just need a bit of a push.

UPDATE

Looking into the source code of the maven archetype project, looks like the following snippet in the settings.xml might work for you:

<servers>
    <server>
      <id>archetype</id>
      <username>${your username}</username>
      <password>${your password}</password>
    </server>
</servers>

There is a default ID of archetype when building the Repository object while fetching a remote catalog. I don't think it's the official way of dealing with such situations, and it's a bit dirty IMO. But it might still work for you :-)

Also, you should be able to set profiles for reusing the archetype ID for different servers.

OTHER TIPS

I think it should be in your settings.xml

<servers>
    <server>
      <id>myrepo</id>
      <username>${your username}</username>
      <password>${your password}</password>
    </server>
</servers>

you need to add <server> for each of password protected repositories.

Looks like this is a known issue and you can't use archetypes from protected repository. See https://issues.apache.org/jira/browse/ARCHETYPE-204

There is a workaround available by doing the following:

mvn archetype:generate -DarchetypeCatalog=https://username:password@maven.mycompany.com/nexus/content/repositories/myrepo/
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top