4

I try to package an application into a jar file with maven. Somehow all files except .gitignore files are added to the jar.

Why is this file skipped and how can I disable this?

Even if I try to include it like below the include is ignored and the jar file remains empty.

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <configuration>
    <includes>
      <include>**/.gitignore</include>
    </includes>
  </configuration>
</plugin>
  • maven-jar-plugin version: 3.1.0
  • maven version: 3.5.2
ochs.tobi
  • 3,214
  • 7
  • 31
  • 52
  • 2
    Why do you want to include .gitignore into a jar? It is useless to java, and within a jar it is useless to git. – PowerStat Apr 04 '19 at 11:51
  • The jar is used as a container to package a development ready sample application in an already existing workflow. – ochs.tobi Apr 04 '19 at 12:00
  • From my point of view you should better use a zip archive for this. – PowerStat Apr 04 '19 at 12:07
  • But I guess that your .gitignore is also not found in the target directory - right? This means you need to copy it to there via the resource plugin - otherwise the jar plugin can't find it. – PowerStat Apr 04 '19 at 12:12
  • I have told the resource plugin to copy the .gitignore file via `addDefaultExcludes` to target. This is working. But the maven-jar-plugin still ignores it. – ochs.tobi Apr 04 '19 at 12:17
  • Please add some information about your project structure and the result that you expect. – PowerStat Apr 04 '19 at 12:30
  • Might be that you must copy the .gitignore into target/classes so that the jar plugin can catch it. – PowerStat Apr 04 '19 at 12:36
  • @ochs.tobi When you say "_to target_" do you mean `target/classes`? Since that's the [JAR plugin's default ``](https://maven.apache.org/plugins/maven-jar-plugin/jar-mojo.html) "_containing the classes and resource files that should be packaged into the JAR_". – Gerold Broser Apr 04 '19 at 12:47
  • My sources are copied to `target/classes`. The .gitignore file is also there. – ochs.tobi Apr 04 '19 at 12:53
  • Every other file can be added with the above code snippet like: `.tfignore` or others only .gitignore seems impossible to add. – ochs.tobi Apr 04 '19 at 13:02

3 Answers3

4

I tried this with a src/main/resources/.gitignore and it worked with the default maven-jar-plugin:2.4, i.e. .gitignore was packaged into the JAR.

Then I used the maven-jar-plugin:3.1.0 you mention and it did not work, as you describe.

It turned out that it doesn't work from v2.5 onwards.

Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
4

I have the same issue with a .metadata folder in the target/classes folder. The .metadata folder is not included in the jar archive. For me, it is not working with maven-jar-plugin:2.4 and upper. With version 2.3 it is working.

I submitted this issue : https://issues.apache.org/jira/browse/MJAR-265

2

The first thing is using a jar file example projects is astonishing. I would never expect to have example projects within a .jar file. The intention of a jar files is something different. I would suggest to use something more appropriate like .zip or .tar.gz etc. (This can be achieved with the maven-assembly-plugin) This will prevent accidental not intended use.

Apart from the whole problem is based on the definition of resources which are usually copied from src/main/resources to the target/classes directory. This is done by the maven-resources-plugin.

The maven-resources-plugin plugin has some kind of configuration which excludes some files which are usually not copied which contains .gitignore. So this means just putting a .gitignore file into src/main/resources will not produce the expected result nor using <includes>..</includes> configuration will not help here as well.

This means you need to change the default configuration of maven-resources-plugin via pluginManagement section like the following:

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.1.0</version>
          <configuration>
            <addDefaultExcludes>false</addDefaultExcludes>
          </configuration>
        </plugin>

Than the .gitignore file will be copied and should be packaged into the resulting jar file (Which I would not recommend to do.)

khmarbaise
  • 92,914
  • 28
  • 189
  • 235
  • 2
    That's right. I already managed to copy the files with the resources plugin to `target/classes`. But the `maven-jar-plugin` > 2.4 also skips `.gitignore` files. – ochs.tobi Apr 05 '19 at 06:17