Search code examples
javagitmavengitignore

Ignore maven target folder but not package "target"


I have a problem managing my gitignore file. My git folder structure looks like this:

  • Project1
    • src
    • target
  • Project2
    • src
      • main
        • java
          • target
    • target
  • Project3
    • src
    • target

.gitignore

I want to ignore all target folders which is generated by maven build process, but not target package(inside Project2) and all files within the packge. I have tried to use this .gitignore:

target/
!Project2/src/main/java/target/

The above example is not working out for me.

The problem is that I have many packges and I dont want to explicit ignore the target folder for each one, but rather point out the target package not to ignore.

*** Renaming the package is not an option.


Solution

  • Try the following:

    **/target/**
    !**/src/**
    

    The first line will add all targets while the second says to exclude src from the previous ignores.

    https://git-scm.com/docs/gitignore