Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven - Is `maven-archetype-simple` a valid archetype?

Usually, I create maven jar project with archetype maven-archetype-quickstart, it works well.

But I want to create Maven project with no sample App.java class, thus I tried maven-archetype-simple archetype, and get error.

Maven command:

mvn archetype:generate -DgroupId=eric -DartifactId=hello -Dversion=0.1 -DarchetypeArtifactId=maven-archetype-simple -DinteractiveMode=false -X -DarchetypeCatalog=local

Error tip:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-archetype-plugin:2.4:generate (default-cli) on project standalone-pom: The defined artifact is not an archetype -> [Help 1] org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-archetype-plugin:2.4:generate (default-cli) on project standalone-pom: The defined artifact is not an archetype at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:212) 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:116) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80) at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51) at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128) at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307) at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193) at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106) at org.apache.maven.cli.MavenCli.execute(MavenCli.java:863) at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:288) at org.apache.maven.cli.MavenCli.main(MavenCli.java:199) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289) at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229) at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415) at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356) Caused by: org.apache.maven.plugin.MojoFailureException: The defined artifact is not an archetype at org.apache.maven.archetype.mojos.CreateProjectFromArchetypeMojo.execute(CreateProjectFromArchetypeMojo.java:205) at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134) at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:207) ... 20 more Caused by: org.apache.maven.archetype.exception.ArchetypeGenerationConfigurationFailure: The defined artifact is not an archetype at org.apache.maven.archetype.ui.generation.DefaultArchetypeGenerationConfigurator.configureArchetype(DefaultArchetypeGenerationConfigurator.java:150) at org.apache.maven.archetype.mojos.CreateProjectFromArchetypeMojo.execute(CreateProjectFromArchetypeMojo.java:189) ... 22 more [ERROR] [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

The questions are:

  • Is maven-archetype-simple a valid archetype?
    In the official doc of maven, it says maven-archetype-simple is a archetype, refer: https://maven.apache.org/guides/introduction/introduction-to-archetypes.html , but it won't work in my test, so get confused.
  • How to create Maven project without the generated code App.java?
like image 938
user218867 Avatar asked Sep 12 '25 17:09

user218867


2 Answers

The artifact maven-archetype-simple does exist on Maven Central, but it isn't a valid archetype since it doesn't containt the right metadata files. A valid archetype must have in its JAR file:

  • either a META-INF/maven/archetype-metadata.xml (this is the new format);
  • or a META-INF/maven/archetype.xml or even a META-INF/archetype.xml (this is the old format).

And that specific artifact, as it is present on Central, doesn't have those files. As such, it isn't considered a valid archetype for the plugin. Those files store the required parameters for the archetype, their possible default values, the files that it should use, etc. so they really are required.

I'm not sure there exists an archetype that would generate just a lone pom.xml with the given Maven coordinates. This is effectively what using the maven-archetype-quickstart, without generating the App.java and AppTest.java would do. Keep in mind that an archetype is really intended at creating a project from a pre-defined template, like a sample Java EE application, or a sample Maven project; all of those would require more set-up than just writing a POM file.

If you really, really, do not want those files, you can either

Create your own archetype

Create a new Maven project, for example my-simple-archetype, with the following directory structure:

pom.xml
src
\---main
    \---resources
        +---archetype-resources
        |       pom.xml
        |
        \---META-INF
            \---maven
                    archetype-metadata.xml

Content of pom.xml at the root:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>eric</groupId>
  <artifactId>my-simple-archetype</artifactId>
  <version>0.1</version>
  <packaging>maven-archetype</packaging>
  <build>
    <extensions>
      <extension>
        <groupId>org.apache.maven.archetype</groupId>
        <artifactId>archetype-packaging</artifactId>
        <version>2.4</version>
      </extension>
    </extensions>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-archetype-plugin</artifactId>
          <version>2.4</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

Content of the src/main/resources/archetype-resources/pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>${groupId}</groupId>
  <artifactId>${artifactId}</artifactId>
  <version>${version}</version>
</project>

And finally, content of the src/main/resources/META-INF/maven/archetype-metadata.xml:

<archetype>
  <id>my-simple-archetype</id>
</archetype>

Now you can build this project and install it:

cd my-simple-archetype
mvn clean install

This will update your local catalog so that this new archetype is available. You can finally use it! In a new directory, do

mvn archetype:generate -DgroupId=eric -DartifactId=hello -Dversion=0.1 -DarchetypeArtifactId=my-simple-archetype -DarchetypeGroupId=eric -DinteractiveMode=false

And you will have as result your wanted project... which consists of the lone pom.xml. So, of course, you can now customize this archetype of yours.

Remove the files

Or you decide that it is not worth the effort, and it is a lot simpler to remove the files after their creation:

mvn archetype:generate -DgroupId=eric -DartifactId=hello -Dversion=0.1 -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
rmdir /S /Q hello\src

Or rm -rf hello/src if you're on a Linux machine.

like image 139
Tunaki Avatar answered Sep 14 '25 06:09

Tunaki


About maven-archetype-simple, even if it would be valid (which isn't the case), it's a little outdated (2006). I suggest you use something more up to date so that plugins and java versions aren't too old.

java8-quickstart-archetype would fit the bill:

mvn archetype:generate -DgroupId=eric -DartifactId=hello \
-Dversion=0.1 -DarchetypeArtifactId=java8-quickstart-archetype \
-DarchetypeGroupId=pl.org.miki -DinteractiveMode=false

This archetype sources can be found here: github.com/mikolak-net/java8-quickstart-archetype.

like image 24
alexbt Avatar answered Sep 14 '25 07:09

alexbt