Search code examples
javamavenbuildpath

Exclude a package from build path in POM


I have a package de.ht.ak.praktikum.hook in a project which is in a source folder src but it should be excluded from build path. I used to do this with right click on it and choosing Build Path -> Exclude. Since I added maven to the project every time I update the project the excluded folder turns into package again, i.e. the exclusion gets deleted. I tried to fix it this way:

...
    <build>
        <sourceDirectory>src</sourceDirectory>
        <resources>
            <resource>
                <directory>src</directory>
                <excludes>
                    <exclude>de/ht/ak/praktikum/hook</exclude>
                </excludes>
            </resource>
        </resources>
    ...
    </build>
...

I tried also to do it as described there:

...
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-eclipse-plugin</artifactId>
        <version>3.1</version>
        <configuration>
            <sourceExcludes>
                <sourceExclude>de/ht/ak/praktikum/hook/</sourceExclude>
            </sourceExcludes>
            <sourceIncludes>
                <sourceInclude>**/*.java</sourceInclude>
            </sourceIncludes>
        </configuration>
    </plugin>
...

However none of the both methods help. Any ideas?


Solution

  • Your first attempt won't work because you're specifying to exclude it as a resource (i.e., those files that get packaged in your resulting JAR file - you don't usually want source files to be among them).

    The second attempt is more on the right track. However, you want to exclude them from compilation, hence you need to set the exclude option of the maven-compiler-plugin. I.e., like this:

    <build>
      ..
      <plugins>
        ..
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.2</version>
          <configuration>
            <excludes>
              <exclude>de/ht/ak/praktikum/hook/*.java</exclude>
            </excludes>
          </configuration>
        </plugin>
        ..
      </plugins>
    </build>
    

    When updating the project in Eclipse (Maven -> Update Project), Eclipse should honor this configuration and also exclude it from the Eclipse-internal build path.