0

Given the below 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>com.myApp</groupId>
    <artifactId>malloc</artifactId>
    <version>0.0.1</version>
    <packaging>war</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <repositories>
        <repository>
            <id>project</id>
            <url>file:///${basedir}/lib</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>com.myApp</groupId>
            <artifactId>myApp.core</artifactId>
            <version>1.0.0</version>
        </dependency>
        ... //other dependencies
    </dependencies>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <resources>
            <resource>
                <directory>src</directory>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                        <addClasspath>true</addClasspath>
                         </manifest>
                         <addMavenDescriptor>false</addMavenDescriptor>
                    </archive>
                    <webResources>
                         <webResource> 
                           <directory>${project.build.directory}/WebContent/WEB-INF</directory> 
                           <includes> 
                             <include>web.xml</include> 
                           </includes> 
                           <targetPath>WEB-INF</targetPath> 
                           <filtering>true</filtering> 
                         </webResource>
                    </webResources>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.10</version>
                <executions>
                    <execution>
                    <id>copy-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                            <outputDirectory>${project.build.directory}/WebContent/WEB-INF/lib</outputDirectory>
                            <includeScope>runtime</includeScope>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

And the following project structure:

enter image description here

I expect the maven-dependency-plugin would copy all the dependencies to the WebContent/WEB-INF/lib but when I run

mvn clean install

The following error is generated:

[INFO] Scanning for projects...
[INFO] 
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building api-malloc 0.0.1
[INFO] ------------------------------------------------------------------------
[WARNING] The POM for com.oracle:ojdbc6:jar:10.2.0.4.0 is missing, no dependency information available
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ api-malloc ---
[INFO] Deleting C:\Development\malloc\target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ api-malloc ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ api-malloc ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 8 source files to C:\Development\malloc\target\classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /C:/Development/malloc/src/com/myApp/api/malloc/dto/RegionTypeDTO.java:[3,34] package com.myApp.api.core.dto does not exist
[ERROR] /C:/Development/malloc/src/com/myApp/api/malloc/dto/RegionTypeDTO.java:[5,39] cannot find symbol
  symbol: class IDTO
[INFO] 26 errors 
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.209 s
[INFO] Finished at: 2015-11-26T15:33:40-05:00
[INFO] Final Memory: 21M/227M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project malloc: Compilation failure: Compilation failure:
[ERROR] /C:/Development/malloc/src/com/myApp/api/malloc/dto/RegionTypeDTO.java:[3,34] package com.myApp.api.core.dto does not exist
[ERROR] /C:/Development/malloc/src/com/myApp/api/malloc/dto/RegionTypeDTO.java:[5,39] cannot find symbol
[ERROR] symbol: class IDTO

[ERROR] location: class com.myApp.api.malloc.controllers.TestController
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

I noticed the WEB-INF/lib folder is always empty, maven dependencies are not copied over.

I am suspecting the copy-dependencies plugin didn't get run before the compile stage but I can't really figure out - anything that I missed?

A_Di-Matteo
  • 26,902
  • 7
  • 94
  • 128
jamesdeath123
  • 4,268
  • 11
  • 52
  • 93
  • Why have you changed the default source folder? Why not going with the [conventions](https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html) ? Furthermore why do you define a repository with `file:///...` which does not make sense, cause the war plugin will automatically pack all dependencies into `WEB-INF/lib`folder automatically.. If you are getting all the jars' from the lib folder to be used as part of your build this is going beyond the idea of Maven... – khmarbaise Nov 26 '15 at 21:36
  • @khmarbaise the file:/// points to a local folder which is not managed by maven by default. – jamesdeath123 Nov 26 '15 at 21:47
  • Which you should change to upload those artifacts to a repository manager and use them as usual dependencies... – khmarbaise Nov 26 '15 at 22:34

1 Answers1

1

The copy-dependencies goal indeed will never be executed if the compilation fails because its binding is to the package phase, which comes after the compile phase (to which by default is linked the Maven Compiler Plugin).

If you want it to run before the compile phase, you need to change the phase value of the execution of the copy-dependencies goal. Changing to process-resources should be fine and should also make sense.

<phase>package</phase>

For a full description of phases, you can check official documentation, here.

You should also fix the compilation errors the build output is pointing at. I see the sourceDirectory element is overriding what Maven uses by default for Java source code (src\main\java), hence I suppose your code is directly under the src folder.

Updated: the package X does not exist error occurs when the code is referring to a package not resolved by the Java compiler, hence it is not able to see that package in the classpath, which means in the declared dependencies: does myApp.core contain that package and class? If yes, then the repositories element (the lib folder) is probably not properly providing the myApp dependency.

You can try to install the dependency locally in your .m2 maven cache using the Maven Install Plugin as specified here. You could execute from the command line as following:

mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file  
       -Dfile=lib\myApp.core-1.0.0.jar \
       -DgroupId=com.myApp \
       -DartifactId=myApp.core \
       -Dversion=1.0.0 \
       -Dpackaging=jar \

Side note: Maven prefers lower case groupid and artifactid tokens. Moreover, instead of using Camel Case (i.e. myApp), Maven convention is also use a dash for separating tokens (i.e. myapp-core).

A_Di-Matteo
  • 26,902
  • 7
  • 94
  • 128
  • hmm, I tried the process-resources but then got the following error in the pom file: Plugin execution not covered by lifecycle configuration: org.apache.maven.plugins:maven-dependency-plugin:2.10:copy-dependencies (execution: copy-dependencies, phase: process-resources) – jamesdeath123 Nov 26 '15 at 21:39
  • But do you really need it to be performed before compilation or you actually wanted to check it regardless of compilation error? Otherwise, you should firstly fix the compilation error and then check the copy-dependencies execution keeping the package phase. – A_Di-Matteo Nov 26 '15 at 21:43
  • Moreover, if Eclipse is giving you that error about execution not covered, you can ignore it, it's a m2e bug (Eclipse Maven integration plugin), always rely on Maven execution from command line. If the Maven build is successful even with the phase at process-resources, you can then ignore Eclipse error (sadly but true). – A_Di-Matteo Nov 26 '15 at 21:45
  • The codes are within the folder: ${basedir}/src/com/myApp/api/malloc - so the packages are defined as this, and that's why the sourceDirecotry is /src/. The com.myApp.api.core.dto that it says missing is coming from the maven dependency repository inside ${basedir}/lib. What do you think is the proper config that I missed? – jamesdeath123 Nov 26 '15 at 21:52
  • Concerning 2), "the package X does not exist error" occurs when the code is refering to a package not resolved by the Java compiler, hence it is not able to see that package in the classpath, which means in the declared dependencies: does myApp.core contain that package and class? If yes, then the repositories element (the lib folder) is probably not properly providing the myApp dependency. You can try to install the dependency locally in your .m2 maven cache using the Maven Install Plugin as specified here https://maven.apache.org/plugins/maven-install-plugin/examples/specific-local-repo.html – A_Di-Matteo Nov 26 '15 at 22:26
  • It says the artifact is already installed. and rerun mvn install still tells me the same error.. – jamesdeath123 Nov 27 '15 at 05:00
  • The other questions seems deserve a separate ticket. You are welcome to visit http://stackoverflow.com/questions/33951180/maven-compile-does-not-recognize-my-local-repo-dependency and provide any helps you have! Thanks! – jamesdeath123 Nov 27 '15 at 06:57