Search code examples
javaeclipsemavenjar.class-file

Missing class files after Maven build


I'm using Maven 3.3.9 on Windows 10. I have a multi-POM project, and I want to share some test resources across the Maven projects. As we all know the files in src/test/* won't be included in distribution JARs, so I created a separate project just for the test resources (and accompanying Java class with the resource names), so that I could share this project across other projects.

For the sake of illustration let's say that there are two files in the foo-test project:

  • src/main/java/com/example/foo/test/FooTestResources.java
  • src/main/resources/com/example/foo/test/resource.txt

Straightforward, right? Except that when I build the project in Maven, the resulting JAR file only contains com/example/foo/test/resource.txt! The class file, which I would expect to be in com/example/foo/test/FooTestResources.class, is missing.

In fact they this file isn't even in the target directory, so it's as if Maven isn't even compiling the source file. I searched my project directory for FooTestResources.class, and it doesn't exist. Only the source code file FooTestResources.java is there.

When I build the project on Eclipse 4.6.3, Eclipse generates the file target/com/example/foo/test/FooTestResources.class as expected. When I do a mvn clean install it disappears.

What is going on?


Solution

  • I found it! Here's the story.

    Once upon a time I had a project that needed two JARs. One JAR would contain the source code along with most of the resources, and the other would contain just a single resource. Rather than creating two projects (and two Git repositories), I tried to be lazy and I created two POMs. One POM built everything normally except for the one resource, the other… (you guessed it) ignored all the source files and generated the POM files.

    You can read the full technical details here: https://globalmentor.atlassian.net/browse/CLOGR-3

    For various reasons that didn't turn out to be the most manageable approach, so I split out the two POMs into separate projects repositories, as they should have been to begin with.

    Unfortunately, somewhere along the way I copied the crucial lines from one of those POMs into this POM.

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <skipMain>true</skipMain>
                    <skip>true</skip>
                </configuration>
            </plugin>
    

    The skipMain is what does it. Bye-bye source files. (Apparently Eclipse ignores the details of this section.)

    I was also skipping tests as well; I basically had just copied from the wrong POM, a POM I don't even have around anymore.