javaeclipsemavenapimaven-tomcat-plugin

Maven: Files missing under classes in maven build


Bear with me, am new to Maven. So, I have this project checked in with the following structure-

-WebContent
-src
-pom.xml

When I build this project through the command line, using the command-

mvn clean
mvn package

and I get a target folder with a .war file which I deployed to the tomcat server. My project UI is displayed but no APIs get called. But when I import the same project to Eclipse IDE and build it, I notice there are an extra set of files and folders-

  • build
  • .classpath
  • ImportedClasses
  • .project
  • .settings

As well as an extra set of .xmls under classes in the target dir.

This is my pom.xml-

<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>
<groupId>GenericApp</groupId>
<artifactId>GenericApp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<dependencies>

    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.22.1</version>
    </dependency>
    <dependency>
        <groupId>asm</groupId>
        <artifactId>asm</artifactId>
        <version>3.3.1</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-server</artifactId>
        <version>2.22.1</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>2.22.1</version>
    </dependency>
    <dependency>
                   <groupId>javax.servlet</groupId>
                   <artifactId>javax.servlet-api</artifactId>
                   <version>3.0.1</version>
                   <scope>provided</scope>
           </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-multipart</artifactId>
        <version>2.22.1</version>
    </dependency>

    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.0</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </dependency>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.4.2</version>
    </dependency>
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.2</version>
    </dependency><!-- https://mvnrepository.com/artifact/commons-net/commons-net -->
    <dependency>
        <groupId>commons-net</groupId>
        <artifactId>commons-net</artifactId>
        <version>2.0</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.8.2</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.8.4</version>
    </dependency>


    <dependency>
        <groupId>io.jsonwebtoken</groupId>
        <artifactId>jjwt</artifactId>
        <version>0.7.0</version>
    </dependency>


    <!-- https://mvnrepository.com/artifact/log4j/log4j -->
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/javax.json/javax.json-api -->
    <dependency>
        <groupId>javax.json</groupId>
        <artifactId>javax.json-api</artifactId>
        <version>1.1.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.json/json -->
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20171018</version>
    </dependency>


</dependencies>

<build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.0.0</version>
            <configuration>
                <warSourceDirectory>WebContent</warSourceDirectory>
            </configuration>
        </plugin>
    </plugins>
</build>

Why does both builds produce seperate results from the same set of files? How do I resolve and understand this problem?


Solution

  • By default, maven package adds the default src by compiling and packaging them and adding default resource into the war file.

    The default location for resources in maven based web-apps is: src/main/resources If your resources are at other location it will not be added to a war file.

    If you wish to include another resource like any .xml file or image etc. you can mention that explicitly in pom.xml for the same.

    Example: inside your build tag of add below:

    <resources>
     <resource>
       <directory>[your folder here]</directory>
     </resource>
    

    The above instruction will add the resources to the root of the folder in war WEB-INF. If you wish to add them to a specific location then you can add

    <resources>
     <resource>
       <directory>[your folder here]</directory>
       <targetPath>[you target path in WEB_INF/classes</targetPath>
     </resource>
    

    Example:

     <resource>
        <directory>src/com/ekagga/ga/config/</directory>
        <targetPath>com/ekagga/ga/config</targetPath>
        <filtering>true</filtering>
      </resource>
    </resources>
    

    Default it will add all the files like .java etc, you may need to add a filter to remove such file.