Maven原型目录:指定自定义位置。

9
我正在部署一个用于Maven的Nexus存储库,并在其中使用自定义原型。
我想执行 mvn archetype:generate并提示一个内部+自定义原型列表。
我发现唯一的以人体工程学方式提示自定义原型的方法(即不使用URL)是将原型目录路径定义为设置中的属性。这不是有效的解决方案,因为我想要多个目录(而且此属性无法在CLI中被覆盖)。
有人知道如何做到这一点吗?
提前感谢。

[编辑] 我发现了一个相关的问题报告: http://jira.codehaus.org/browse/ARCHETYPE-273

我注意到在 archetype:generate 过程中,Maven 试图访问中央仓库:

[DEBUG] Searching for remote catalog: http://repo1.maven.org/maven2/archetype-catalog.xml
[DEBUG] Searching for remote catalog: http://repo1.maven.org/maven2

由于我没有指定代理,所以它以“连接超时”结束...。
我不明白为什么Maven不检查Nexus目录...。

有什么新消息吗?我这里也遇到了同样的问题 :( - Ruben
3个回答

6

我还配置了一个Nexus来镜像Maven仓库,因此也包括远程仓库目录。

<mirror>
    <!--This sends everything else to /public -->
    <id>nexus</id>
    <mirrorOf>*</mirrorOf>
    <url>http://afbwt03:8081/nexus/content/groups/JavaRepo/</url>
</mirror>

并且:

<profile>
    <id>nexus</id>
    <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>

只有在使用以下Maven命令行时,我才能访问远程目录:

mvn archetype:generate -DarchetypeCatalog=http://afbwt03:8081/nexus/content/groups/JavaRepo

如果我不定义archetypeCatalog变量,我会得到与您相同的行为:尝试访问repo1......尽管已经配置了一些镜像。


很遗憾,从Maven 3开始(我相信),无法通过在命令行中传递目录的URL或文件名来执行“mvn archetype:generate -DarchetypeCatalog = ...”。只支持本地、远程和内部,并且必须在settings.xml中进行配置。详见:https://maven.apache.org/archetype/maven-archetype-plugin/generate-mojo.html#archetypeCatalog。 - Alexander Klimetschek

2

您需要在活动配置文件中的.m2/settings.xml中定义属性archetypeRepository

  • 将repositories和pluginRepositories重定向到与id“central”相同的镜像。

  • 当然,还需要定义镜像。

Apache Maven原型插件文档指定archetypeRepository是可定义用户属性: http://maven.apache.org/archetype/maven-archetype-plugin/generate-mojo.html

您的.m2/settings.xml应具有这些最小元素

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

<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">


  <mirrors>
      <mirror>
        <id>central</id>
        <name>Mirror for maven central</name>
        <url>http://mymvnhost:8081/nexus/content/groups/public/</url>
        <mirrorOf>*</mirrorOf>
      </mirror>

  </mirrors>

  <profiles>
    <profile>
      <id>dev</id>

        <properties>
        <archetypeRepository>http://mymvnhost:8081/nexus/content/groups/public/</archetypeRepository>
        </properties>

        <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>
    <activeProfile>dev</activeProfile>
  </activeProfiles>
</settings>

0

使用 maven-archetype-plugin:3.1.1,需要:

  • 在你的仓库中添加/编辑 archetype-catalog.xml 文件,并列出你自定义的原型。
  • 编辑你的 settings.xml 文件,将此仓库的 ID 设置为 archetype
  • 运行命令 mvn archetype:generate -DarchetypeCatalog=remote

引用自 https://maven.apache.org/archetype/maven-archetype-plugin/generate-mojo.html

If you want the catalogs to come from a different repository, please add the following to your settings.xml

<repository>
  <id>archetype</id>
  <url>https://repository.domain.com/path/to/repo/</url>
</repository>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接