Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven archetype plugin doesn't let .resources in archetype-resources through

How can I make resources like .gitignore be part of the resulting project?

  1. create archetype with archetype-resources/.gitignore
  2. mvn install
  3. mvn archetype:generate
  4. resulting project doesn't contain .gitignore

PS. I'm sure it isn't there.

like image 393
lisak Avatar asked Nov 02 '11 13:11

lisak


People also ask

Which maven plugin is used for archetype generation?

The Archetype Plugin allows the user to create a Maven project from an existing template called an archetype. It also allows the user to create an archetype from an existing project. This plugin requires Java 7.

Which command will provide the list of archetypes?

the archetype should now be listed in ~/. m2/archetype-catalog. xml or /path/to/local/mvn/repo/archetype-catalog. xml.

What is archetype selection in maven?

In short, Archetype is a Maven project templating toolkit. An archetype is defined as an original pattern or model from which all other things of the same kind are made. The name fits as we are trying to provide a system that provides a consistent means of generating Maven projects.


6 Answers

The bug seems to be still present in the maven-archetype-plugin v3.0.1 . For those who do not want to downgrade the maven-resource-plugin. I managed to establish a more or less ugly workaround.

First you rename the archetype-resources/.gitignore to

__gitignore__

then inside the archetype-metadata.xml add

<requiredProperties>
    <requiredProperty key="gitignore">
        <defaultValue>.gitignore</defaultValue>
    </requiredProperty>
</requiredProperties>

<fileSets>
    <fileSet>
        <directory></directory>
        <includes>
            <include>__gitignore__</include>
        </includes>
    </fileSet>
</fileSets>

When the archetype is generated maven will now first copy the __gitignore__ then sees the __[file]__ syntax and will replace it with the default value ".gitignore"

like image 131
PODerik Avatar answered Oct 07 '22 07:10

PODerik


This solution for upcoming maven-resources-plugin v3.0.0 (not yet released at the time of posting this; current is still 2.7) from https://issues.apache.org/jira/browse/MRESOURCES-190 seems better than holding back version upgrades:

<build>
  <plugins>
    <plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-resources-plugin</artifactId>
       <configuration>
          <!-- Required so that .gitignore gets included in archetypes; see https://issues.apache.org/jira/browse/MRESOURCES-190 -->
          <addDefaultExcludes>false</addDefaultExcludes>
like image 42
vorburger Avatar answered Oct 07 '22 08:10

vorburger


Add a fileSet entry to src/main/resources/META-INF/maven/archetype-metadata.xml with an empty directory tag:

<fileSet>
  <directory></directory>
  <includes>
    <include>.gitignore</include>
  </includes>
</fileSet>

This will copy the included files from src/main/resources/archetype-resources to the project root directory.

like image 30
Marc Klinger Avatar answered Oct 07 '22 07:10

Marc Klinger


Check your maven-resources-plugin version by launching the Maven build on debug (with -X option). If you use 2.7, there is a regression where .gitignore files are silently ignored.

In this case, you will have to explicitly use 2.6 in your pom.xml:

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.6</version>
            </plugin>
        </plugins>
    </pluginManagement>
</build>
like image 30
jurevert Avatar answered Oct 07 '22 08:10

jurevert


ARCHETYPE/issues/ARCHETYPE-505 shows that this is thoroughly broken with current versions of the plugin and maven. No workarounds help anymore with this.

like image 20
dschulten Avatar answered Oct 07 '22 07:10

dschulten


Solution that worked perfectly for me was to use archetype post install groovy script.

Create a file META-INF/archetype-post-generate.groovy in the resources folder of your archetype project.

Add this code:

file = new File( request.getOutputDirectory(), request.getArtifactId()+"/.gitignore.tmpl" );
def gitIgnorefile = new File( request.getOutputDirectory(), request.getArtifactId()+"/.gitignore" );
file.renameTo(gitIgnorefile)

In your archetype-metadata.xml file include the template .gitignore.tmpl file.

        <fileSet>
            <directory/>
            <includes>
                <include>.gitignore.tmpl</include>
            </includes>
        </fileSet>

I had problems with maven resource plugin hence used groovy script solution.

like image 45
Amrut Prabhu Avatar answered Oct 07 '22 07:10

Amrut Prabhu