简体   繁体   中英

Maven: project compiles without dependency defined in pom.xml

I have a project that I tried building in two different ways:

  • With -Dmaven.repo.local set to a brand new directory created just for the build.
  • Without -Dmaven.repo.local (then I assume it defaults to ~/.m2

I'm finding it very weird that my project compiles just fine when I am NOT using -Dmaven.repo.local , even though some dependencies are not in the POM . The log does NOT show these dependencies being downloaded or resolved from the local repo ( ~/.m2 ).

If I try to build specifying a brand new local repository, the build fails because javac can't resolve some classes (meaning Maven and Java don't know where these dependencies are).

[ERROR] /home/jenkins1/slave_home/workspace/Build/2.x/Dev_Test/2-Mainline_Stream_official_Green_build_V2/workspace/com.mycompany.myproj.iaas.rest/src/main/java/com/mycompany/myproj/iaas/api/rest/MigrationUninstallModulesResource.java:[18,23] error: package javax.ws.rs.core does not exist
[ERROR] /home/jenkins1/slave_home/workspace/Build/2.x/Dev_Test/2-Mainline_Stream_official_Green_build_V2/workspace/com.mycompany.myproj.iaas.rest/src/main/java/com/mycompany/myproj/iaas/api/rest/MigrationUninstallModulesResource.java:[42,1] error: cannot find symbol
[ERROR] symbol: class Path
[ERROR] /home/jenkins1/slave_home/workspace/Build/2.x/Dev_Test/2-Mainline_Stream_official_Green_build_V2/workspace/com.mycompany.myproj.iaas.rest/src/main/java/com/mycompany/myproj/iaas/api/rest/assets/MigrationUninstallModulesAsset.java:[16,18] error: package javax.ws.rs does not exist
[ERROR] /home/jenkins1/slave_home/workspace/Build/2.x/Dev_Test/2-Mainline_Stream_official_Green_build_V2/workspace/com.mycompany.myproj.iaas.rest/src/main/java/com/mycompany/myproj/iaas/api/rest/assets/MigrationUninstallModulesAsset.java:[17,23] error: package javax.ws.rs.core does not exist
[ERROR] /home/jenkins1/slave_home/workspace/Build/2.x/Dev_Test/2-Mainline_Stream_official_Green_build_V2/workspace/com.mycompany.myproj.iaas.rest/src/main/java/com/mycompany/myproj/iaas/api/rest/assets/MigrationUninstallModulesAsset.java:[19,41] error: package org.apache.wink.common.annotations does not exist
[ERROR] /home/jenkins1/slave_home/workspace/Build/2.x/Dev_Test/2-Mainline_Stream_official_Green_build_V2/workspace/com.mycompany.myproj.iaas.rest/src/main/java/com/mycompany/myproj/iaas/api/rest/assets/MigrationUninstallModulesAsset.java:[23,1] error: cannot find symbol
[ERROR] symbol: class Asset
[ERROR] /home/jenkins1/slave_home/workspace/Build/2.x/Dev_Test/2-Mainline_Stream_official_Green_build_V2/workspace/com.mycompany.myproj.iaas.rest/src/main/java/com/mycompany/myproj/iaas/api/rest/assets/TeamAsset.java:[12,18] error: package javax.ws.rs does not exist
[ERROR] -> [Help 1]

As you can see, the build fails to find javax.ws.rs , which is expected since the dependency is NOT in the POM. What I don't understand is why the build succeeds when I suppress the -Dmaven.local.repo .

The POM:

<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>

    <parent>
        <groupId>com.mycompany.myproj.iaas</groupId>
        <artifactId>root</artifactId>
        <version>2.1.0</version>
        <relativePath>../com.mycompany.myproj.iaas</relativePath>
    </parent>

    <groupId>com.mycompany.myproj.iaas</groupId>
    <artifactId>rest</artifactId>
    <packaging>war</packaging>
    <name>myproj myproj-RestAPI.war</name>
    <url>http://www.mycompany.com</url>

    <dependencies>
       <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-junit4</artifactId>
        <version>1.6.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-api-easymock</artifactId>
        <version>1.6.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.easymock</groupId>
        <artifactId>easymock</artifactId>
        <version>3.2</version>
        <scope>test</scope>
    </dependency>
        <dependency>
            <groupId>com.mycompany.myproj.iaas</groupId>
            <artifactId>common</artifactId>
            <version>${project.parent.version}</version>
        </dependency>
        <dependency>
            <groupId>com.mycompany.myproj.iaas</groupId>
            <artifactId>bpm</artifactId>
            <version>${project.parent.version}</version>
        </dependency>
        <dependency>
            <groupId>com.mycompany.myproj.iaas</groupId>
            <artifactId>sql</artifactId>
            <version>${project.parent.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.wink</groupId>
            <artifactId>wink-server</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-jaxrs</artifactId>
            <version>1.7.4</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-xc</artifactId>
            <version>1.7.4</version>
        </dependency>
        <dependency>
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.1</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${version.slf4j}</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>${version.logback}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jul-to-slf4j</artifactId>
            <version>${version.slf4j}</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
        </dependency>
        <dependency>
            <groupId>org.jdom</groupId>
            <artifactId>jdom</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.wink</groupId>
            <artifactId>wink-json-provider</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.wink</groupId>
            <artifactId>wink-json4j</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>net.ripe.commons</groupId>
            <artifactId>commons-ip-math</artifactId>
            <version>1.21.1</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
           <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.ibm.db2</groupId>
            <artifactId>db2jcc4</artifactId>
            <version>10.5</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.ibm.db2</groupId>
            <artifactId>db2jcc_license_cu</artifactId>
            <version>10.5</version>
            <scope>test</scope>
        </dependency>

    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <archive>
                        <addMavenDescriptor>false</addMavenDescriptor>
                        <manifest>
                            <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                        </manifest>
                        <manifestEntries>
                            <Implementation-Version>${project.version}.${buildLabel}</Implementation-Version>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <parallel>methods</parallel>
                    <threadCount>10</threadCount>
                </configuration>
                <version>2.17</version>
            </plugin>
        </plugins>
    </build>

    <description>This component builds the myproj-RestAPI.war file.</description>
</project>

It is very confusing - if the dependency is not defined in the POM, how is it being resolved?

You definitely missed a part of Maven log when it tries to resolve all dependencies. Your log shows that it is a Compiler errors not Maven.

It always goes that way:

  1. Maven resolves all dependencies including transitive ones (if they are not excluded)
  2. For those dependencies Maven cannot find in local repo and cannot dowload from remote repos there is a warning about it. (you missed it). Also Maven may not try to download them again for some period of time - it depends on remote repo settings). Maven puts some "empty decoy" into local repo for missing dependencies.
  3. Maven still going to Compiler with all available jars in Classpath just because if jar defined as dependency it does not mean that it is needed and code may compiles.
  4. Finally you got your Errors from Compiler because code actually uses some classes from some dependencies That's it.

BTW: I do not understand completely the purpose of that exercise...

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