mavengroovymaven-archetypekafka-topicgroovy-grape

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


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:


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

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 tag of maven-archetype-plugin. It doesn´t work.

I would appreciate every answer.

Thanks a lot!


Solution

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