Dependencies in archetype-post-generation.groovy script cannot be resolved

huangapple go评论60阅读模式
英文:

Dependencies in archetype-post-generation.groovy script cannot be resolved

问题

以下是您要翻译的内容:

我在从自定义 Maven 原型创建 Maven 项目时遇到了一些问题。我有一个符合 Maven 原型的预期目录结构,其中包括一个生成后的 Groovy 脚本,我希望在项目生成后立即在代理服务器中创建一些 Kafka 主题。

原型项目的结构如下:

/src/main/resources/archetype-resources/ MY_PROJECT_DIRECTORY_STRUCTURE
                   /META-INF----------------/maven/-archetype-metatada.xml
                                            /archetype-post-generate.groovy

target/
pom.xml

我的 pom.xml 结构如下:

<project>
  ...
  <packaging>maven-archetype</packaging>
  ...
  <build>
    <extensions>
      <extension>
        <groupId>org.apache.maven.archetype</groupId>
        <artifactId>archetype-packaging</artifactId>
        <version>3.2.1</version>
      </extension>
    </extensions>
 
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-archetype-plugin</artifactId>
          <version>3.2.1</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
  <dependencies>
    <dependency>
      <groupId>org.apache.kafka</groupId>
      <artifactId>kafka-clients</artifactId>
      <version>3.4.0</version>
    </dependency>
  </dependencies>
</project>

如果我运行 mvn clean install,它可以正常工作。原型被创建并存储在我的本地目录中。

然而,如果我尝试从该原型创建一个新项目(使用 mvn archetype:generate 并输入所有内容,如 artifactId、archetypeArtifactId 等等),我会遇到以下问题:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-archetype-plugin:3.2.1:generate (default-cli) on project standalone-pom: startup failed:
[ERROR] Script1.groovy: 9: unable to resolve class org.apache.kafka.common.errors.TopicExistsException
[ERROR] @ line 9, column 1.

依赖项无法解析。我应该以其他方式添加依赖项来实现这个目的吗?对于生成后的 Groovy 脚本来说有所不同吗?
是否不可能向生成后的脚本添加依赖项?
如果我在我的 IntelliJ IDE 中使用 "Run" 运行脚本,它可以完美地将依赖项添加到 pom.xml 中。
我还尝试将依赖项放在 <plugin> maven-archetype-plugin 的 <dependencies> 标签中,但不起作用。

我将不提供问题的回答,只提供您要翻译的部分。

英文:

I have some problem when creating a maven project from a maven custom archetype. I have the expected directory structure for a maven-archetype, including a post-generation groovy script, which I want to create some Kafka topics in a broker immediately after the project generation.

The structure of the archetype project is:

/src/main/resources/archetype-resources/ DIRECTORY_STRUCTURE_FOR_MY_PROJECT
                   /META-INF----------------/maven/-archetype-metatada.xml
                                            /archetype-post-generate.groovy

target/
pom.xml

My pom.xml has the structure below:


&lt;project&gt;
  ...
  &lt;packaging&gt;maven-archetype&lt;/packaging&gt;
  ...
  &lt;build&gt;
    &lt;extensions&gt;
      &lt;extension&gt;
        &lt;groupId&gt;org.apache.maven.archetype&lt;/groupId&gt;
        &lt;artifactId&gt;archetype-packaging&lt;/artifactId&gt;
        &lt;version&gt;3.2.1&lt;/version&gt;
      &lt;/extension&gt;
    &lt;/extensions&gt;
 
    &lt;pluginManagement&gt;
      &lt;plugins&gt;
        &lt;plugin&gt;
          &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
          &lt;artifactId&gt;maven-archetype-plugin&lt;/artifactId&gt;
          &lt;version&gt;3.2.1&lt;/version&gt;
        &lt;/plugin&gt;
      &lt;/plugins&gt;
    &lt;/pluginManagement&gt;
  &lt;/build&gt;
&lt;dependencies&gt;
        &lt;dependency&gt;
    &lt;groupId&gt;org.apache.kafka&lt;/groupId&gt;
    &lt;artifactId&gt;kafka-clients&lt;/artifactId&gt;
    &lt;version&gt;3.4.0&lt;/version&gt;
         &lt;/dependency&gt;
&lt;/dependencies&gt;
&lt;/project&gt;

If I run mvn clean install, it works properly. The archetype is created and stored in my local catalog.

However, if I try to create a new project from that archetype (using mvn archetype:generate and introducing all stuff.. artifactId, archetypeArtifactId....), I come across this:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-archetype-plugin:3.2.1:generate (default-cli) on project standalone-pom: startup failed:
[ERROR] Script1.groovy: 9: unable to resolve class org.apache.kafka.common.errors.TopicExistsException
[ERROR] @ line 9, column 1.

The dependencies can´t be resolved. Should I add the dependencies in another way for this purpose? Is it different for a post-generation groovy script?
Is it impossible to add dependencies to a post-generation script?
If I run the script from my IntelliJ IDE with "Run" , it works perfectly adding dependencies in pom.xml.
I´ve tried also putting the dependencies inside <dependencies> tag of <plugin> maven-archetype-plugin. It doesn´t work.

I would appreciate every answer.

Thanks a lot!

答案1

得分: 1

只添加了 @Grab("groupId:artifactId:version") 到每个依赖项,并在每个 @Grab 注释下方添加了 import,然后它就可以正常工作了。

英文:

Just added @Grab("groupId:artifactId:version") for each dependency and the import below each @Grab notation and it worked.

huangapple
  • 本文由 发表于 2023年5月24日 22:15:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76324527.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定