简体   繁体   中英

Excluding Source Directory in Maven

Is there a way to exclude a source directory in Maven? I've searched on this topic and find plenty of discussion about excluding Resources, but very little on excluding Sources.

I have a repo with shared projects and several POM files. Most coexist well, but one project has a conflict with one of the source directories and I need to exclude it only from that pom-file.

<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    ...
    <build>
        <sourceDirectory>src/main/java</sourceDirectory>

        # I would like to add this but is not supported
        <excludeSourceDir>src/main/java/com/company/project/conflict-dir</excludeSourceDir>

        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>

Edit 1: Following the recommendations, I implemented a parent-pom and converted the existing pom files into modules -- each module referencing the parent (excellent instructions here ). I also implemented a "maven-compiler-plugin" which allows one to <exclude> files. Most projects compile and package correctly, except the original troublemaker project is still refusing to compile. Still researching options.

After many hours on this and after converting the code to use modules, I found two things:

  • Instead of excluding files, I needed to specify the includes instead:
<artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.0</version>
    <configuration>
        <source>${maven.compiler.source}</source>
        <target>${maven.compiler.source}</target>
        <encoding>UTF-8</encoding>
        <includes>
            <include>com/company/my_module/*.java</include>
        </includes>
    </configuration>
</plugin>
  • The test code from the other modules was being run by default. Yet this modules was not including the code for those tests. I added -Dmaven.test.skip=true to prevent that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM