Error installing a multimodule archetype created with mvn archetype:create-from-project

ε祈祈猫儿з 提交于 2020-07-18 18:42:30

问题


I have a project with the following structure:

project (pom)
project-client (module, jar)
project-ejb (module, jar)
project-web (module, war)
project-ear (module, ear)

Inside the pom the modules reference the parent using:

<relativePath>../project</relativePath>

And the root pom references the modules the same way (using ../).

I can create an archetype using archetype:create-from-project, and everything runs smoothly.

But when I try to install the archetype (mvn install inside the target/generated-sources/archetype dir), I get the error:

    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-archetype-plugin:2.2:integration-test (default-integration-test) on project seguranca-archetype:
    [ERROR] Archetype IT 'basic' failed: org.apache.maven.archetype.exception.ArchetypeGenerationFailure: Error merging velocity templates: Unable to find resource 'archetype-resources/../__rootArtifactId__-client/pom.xml'
    [ERROR] -> [Help 1]
    org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-archetype-plugin:2.2:integration-test (default-integration-test) on project seguranca-archetype: 
    Archetype IT 'basic' failed: org.apache.maven.archetype.exception.ArchetypeGenerationFailure: Error merging velocity templates: Unable to find resource 'archetype-resources/../__rootArtifactId__-client/pom.xml'
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:217)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:319)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
        at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
        at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:616)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
        at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
        at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
    Caused by: org.apache.maven.plugin.MojoExecutionException: 
    Archetype IT 'basic' failed: org.apache.maven.archetype.exception.ArchetypeGenerationFailure: Error merging velocity templates: Unable to find resource 'archetype-resources/../__rootArtifactId__-client/pom.xml'
        at org.apache.maven.archetype.mojos.IntegrationTestMojo.execute(IntegrationTestMojo.java:268)
        at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
        ... 19 more

The problem is that the file exists (it's on target/generated-sources/archetype/src/main/resources/__rootArtifactId__client/pom.xml), and I tried to fiddle with the archetype-metadata.xml with no success. Any clues?

Thanks in advance


回答1:


It looks like the archetype plugin doesn't support flat layout for multi-module projects. If that is your case a workaround is to change to a nested layout. I did it by moving the modules outside the archetype-resources directory into it. Like this:

Before:

pom.xml
src
├── main
│   └── resources
│       ├── archetype-resources
│       │   └── pom.xml
│       ├── META-INF
│       │   └── maven
│       │       └── archetype-metadata.xml
│       ├── __rootArtifactId__.module1
│       │   ├── pom.xml
│       │   └── ...
│       ├── __rootArtifactId__.module2
│       │   ├── pom.xml
│       │   └── ...
│       ├── __rootArtifactId__.module3
│           ├── pom.xml
│           └── ...
│
└── test
    └── resources
        └── projects...

After:

pom.xml
src
├── main
│   └── resources
│       ├── archetype-resources
│       │   ├── pom.xml
│       │   ├── __rootArtifactId__.module1
│       │   │   ├── pom.xml
│       │   │   └── ...
│       │   ├── __rootArtifactId__.module2
│       │   │   ├── pom.xml
│       │   │   └── ...
│       │   └── __rootArtifactId__.module3
│       │       ├── pom.xml
│       │       └── ...
│       └── META-INF
│           └── maven
│               └── archetype-metadata.xml
└── test
    └── resources
        └── projects...

And then you edit any reference to the modules in archetype-metadata.xml, removing the '..' relative path.




回答2:


There is a similar issue to this in the Maven Archetype JIRA: https://issues.apache.org/jira/browse/ARCHETYPE-422

As of this writing, it remains open.

In order to get a working archetype for my project, I have taken jboss-javaee6-webapp-ear-blank-archetype-7.1.3.CR1.jar and generated a project using it. I then modified the project to remove many of the jboss specific items since we are using WebSphere. After these modifications, I run the create from project:

mvn clean archetype:create-from-project -Dinteractive=true

I modify the archetype.groupId and archetype.artifactId to set the location I want the archetype to end up in the repository and its name. I also modify the package value but accept the defaults for the other items.

Much like you experienced this seems to run fine. When I cd to \target\generated-sources\archetype and run mvn install, the jar file is created with the appropriate path and name.

I then use Eclipse to generate a project, and this works fine as well. Great, right?

So I make some small changes to be more specific to our environment, and run through the above steps using the same group and artifact IDs. I then use Eclipse to generate a project, and it gives the error:

org.apache.maven.archetype.exception.ArchetypeGenerationFailure: Error merging velocity templates: Unable to find resource 'archetype-resources/pom.xml'

(I do not consistently get the same error on the command line for mvn archetype:generate, but I have seen it occur.)

For some reason, running through the steps again and entering a different archetype.artifactId, when creating from project, resolves this issue. There appears to be something that gets corrupted or changed when the archetype is getting updated that m2e does not like.

Anyway, this might be worth giving a try. I will see if I can narrow down the issue, but for now I will be using unique artifactIds for each update.




回答3:


The problem appears to occur only when using the Eclipse m2e plugin. I was able to solve the problem using the following command line:

mvn archetype:generate


来源:https://stackoverflow.com/questions/8609198/error-installing-a-multimodule-archetype-created-with-mvn-archetypecreate-from

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!