Maven Build is not including .class file in jar

27,126

I found my problem. The problem was the folder structure. Maven assumes a /src/main/java/ structure. my structure was different and was not overridden in the POM.xml. matching the standard path or overridding the path in the POM.xml fixed the problem.

Share:
27,126

Related videos on Youtube

John Blakie
Author by

John Blakie

Updated on June 20, 2020

Comments

  • John Blakie
    John Blakie almost 4 years

    I have looked through everything I can find on StackOverflow and I could not find an answer. I have just built out a new machine and I have projects that compiled and built on previous installations just fine. But my new machine won't. Everything appears to be building correctly (no build errors, dependencies included properly), but the actual class files (code I wrote) from my project are not included in the jar. I then created a simple helloWorld project with a some what minimal POM file to see of the problem still exists. I did the build from the command line to take eclipse out of the equation.

    Class Code

    package com.zoverge.utils;
    
    public class HelloWorld {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            String message = args[0];
            System.out.println(message);
        }
    
    }
    

    My 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/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.zoverge.utils</groupId>
      <artifactId>helloworld</artifactId>
      <packaging>jar</packaging>
      <version>1.0</version>
      <name>Hello World</name>
      <url>http://maven.apache.org</url>
      <properties>
                <project.build.java.version>1.6</project.build.java.version>
                <org.apache.maven.plugins.maven-compiler-plugin.version>2.5.1</org.apache.maven.plugins.maven-compiler-plugin.version>          
      </properties>
      <build>
            <plugins>
                <!-- Maven will compile our source java classes using the "project.build.java.version" 
                    specified -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>${org.apache.maven.plugins.maven-compiler-plugin.version}</version>
                    <configuration>
                        <source>${project.build.java.version}</source>
                        <target>${project.build.java.version}</target>
                    </configuration>
                </plugin>
      <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.3</version>
            <configuration>
              <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
              </descriptorRefs>
              <archive>
                <manifest>
                  <mainClass>com.zoverge.utils.HelloWorld</mainClass>
                </manifest>
              </archive>
                    <finalName>HelloWorld</finalName>
                    <appendAssemblyId>false</appendAssemblyId>
            </configuration>
            <executions>
              <execution>
                <id>make-assembly</id>
                <phase>package</phase>
                <goals>
                  <goal>single</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
      </plugins>
      </build>
    <dependencies/>
    </project>
    

    The resulting build produces 2 jar files (as expected) helloworld-1.0.jar and HelloWorld.jar. Neither jar contains the compiled class file for HelloWorld.java. I think this has to be a problem with my environment because this behavior is the same for other projects that worked fine on other dev machines, but I can't seem to figure out the difference. I do not have access to my previous dev machine to compare the configuration. My .m2/setting.xml is basically empty.

    <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
                          http://maven.apache.org/xsd/settings-1.0.0.xsd">
      <localRepository>${user.home}/.m2/repository</localRepository>
      <interactiveMode/>
      <usePluginRegistry/>
      <offline/>
      <pluginGroups/>
      <servers/>
      <mirrors/>
      <proxies/>
      <profiles/>
      <activeProfiles/>
    </settings>
    
    • zaid hussian
      zaid hussian over 11 years
      Weird, works for me with Maven 3.0.4.
    • John Blakie
      John Blakie over 11 years
      After digging deeper into my maven installation I found multiple instances (3.0.3 & 2.2.1). I need to clean this up and try again... thx.
    • John Blakie
      John Blakie over 11 years
      No luck. I did a complete uninstall and reinstall of Maven and upgraded to the latest (3.0.4) and I still get a jar with no class files and now the manifest has no main-class listed either. Manifest-Version: 1.0 Archiver-Version: Plexus Archiver Created-By: Apache Maven Built-By: johnblakie Build-Jdk: 1.6.0_37
    • John Blakie
      John Blakie over 11 years
      One additional note. I'm on MacOS 10.7.5, Java version 1.6.0_37.
    • John Blakie
      John Blakie about 11 years
      I did a clean set up on Windows and I get the same result. No class files. Straight Maven 3.0.4 & Java 1.6.0_39. Are you sure you got actual class files and not just a reference. The only directory output in the jar is a properties and a pom.xml file in the META-INF directory. No directories for com/zoverge/utils/HelloWorld.class as I would expect.
    • gilad hoch
      gilad hoch almost 11 years
      I may have encountered the same problem: stackoverflow.com/questions/17092892/… but i could not figure out what's in common to both pom.xml files, that might result with no .class files in the output jar...
  • nemisis enforcer
    nemisis enforcer over 8 years
    wow, this was so dumb of me to miss but was exactly my issue, thanks!!
  • Gernot
    Gernot almost 4 years
    <sourceDirectory>src</sourceDirectory>