Maven:项目编译没有在pom.xml中定义的依赖项

时间:2017-02-17 23:15:03

标签: java maven

我有一个项目,我尝试用两种不同的方式构建:

  • -Dmaven.repo.local设置为仅为构建创建的全新目录。
  • 没有-Dmaven.repo.local(我假设默认为~/.m2

我发现很奇怪我的项目在我不使用-Dmaven.repo.local时编译得很好,即使 某些依赖项不在POM中 。日志不会显示从本地仓库(~/.m2)下载或解析的这些依赖项。

如果我尝试构建指定一个全新的本地存储库,则构建失败,因为javac无法解析某些类(这意味着Maven和Java不知道这些依赖项在哪里)。

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

正如您所看到的,构建无法找到javax.ws.rs,这是预期的,因为依赖关系不在POM中。我不明白的是,当我压制-Dmaven.local.repo时,为什么构建成功。

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>

这非常令人困惑 - 如果POM中没有定义依赖关系,它是如何解决的?

1 个答案:

答案 0 :(得分:0)

当你尝试解决所有依赖项时,你肯定错过了Maven日志的一部分。 您的日志显示它是编译器错误而不是Maven。

总是这样:

  1. Maven解析所有依赖项,包括可传递的依赖项(如果不排除它们)
  2. 对于那些依赖,Maven在本地仓库中找不到,并且不能从远程仓库下载,有关于它的警告。 (你错过了)。 Maven可能也不会尝试在一段时间内再次下载它们 - 这取决于远程仓库设置)。 Maven将一些“空诱饵”放入本地仓库以寻找缺失的依赖关系。
  3. Maven仍然使用Classpath中的所有可用jar进入Compiler 只是因为如果jar被定义为依赖,那并不意味着它是必需的并且代码可以编译。
  4. 最后,您从编译器获得了错误,因为代码实际上使用了某些依赖项中的某些类 就是这样。
  5. 顺便说一句:我完全不明白这项运动的目的......