依赖关系jar版本在maven和gradle构建中有所不同

时间:2017-08-04 10:37:05

标签: java maven gradle war

我正在尝试将我的项目从gradle构建转换为maven构建。我能够从maven生成war文件,但是maven中的某些依赖jar的版本与gradle的版本不同。

的build.gradle:

    buildscript {
        repositories {
            mavenCentral()
        }
        dependencies {
            classpath('org.springframework.boot:spring-boot-gradle-plugin:1.5.2.RELEASE')
            classpath('org.apache.hive:hive-jdbc:2.0.0')
            classpath('postgresql:postgresql:9.1-901-1.jdbc4')
            classpath('org.apache.logging.log4j:log4j-core:2.8.2')
        }
    }

    apply plugin: 'eclipse'
    apply plugin: 'org.springframework.boot'
    bootRepackage {
    mainClass = 'com.rest.api.app'
    }
    apply plugin: 'war'
    apply plugin: 'maven'

    war {
        baseName = 'app'
    }

    repositories {
        mavenCentral()
    }

    configurations {
        all*.exclude module: 'slf4j-log4j12'  
        all*.exclude module: 'log4j-slf4j-impl'
    }
    sourceCompatibility = 1.8
    targetCompatibility = 1.8

    dependencies {
        compile('org.springframework.boot:spring-boot-starter-web'){
            exclude module : 'spring-boot-starter-logging'
        }
        providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
        compile('org.apache.lucene:lucene-snowball:3.0.3')
        compile('org.apache.hive:hive-jdbc:2.0.0')
        compile('postgresql:postgresql:9.1-901-1.jdbc4')
        compile('com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.8.8')
        compile('com.fasterxml.jackson.core:jackson-databind:2.8.8.1')
        compile('org.apache.logging.log4j:log4j-core:2.8.2'){
            force=true
        }   
        compile('org.apache.logging.log4j:log4j-api:2.8.2'){
            force=true
        }
    compile group: 'org.apache.solr', name: 'solr-solrj', version: '5.2.1'
    compile group: 'org.apache.lucene', name: 'lucene-analyzers-common', version: '6.4.0'
    compile group: 'org.apache.httpcomponents', name: 'httpcore', version: '4.4.6'
    compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.3.4'
    compile group: 'org.apache.commons', name: 'commons-text', version: '1.0'
    }

task writeNewPom {
    doLast {
        pom {
            project {
            }
        }.writeTo("$buildDir/mavenpom.xml")
    }
}

的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.app</groupId>
    <artifactId>app</artifactId>
    <version>1.0</version>
    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.5.2.RELEASE</version>
            <scope>compile</scope>
            <exclusions>
                <exclusion>
                    <artifactId>log4j-slf4j-impl</artifactId>
                    <groupId>*</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>slf4j-log4j12</artifactId>
                    <groupId>*</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>spring-boot-starter-logging</artifactId>
                    <groupId>*</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-pool/commons-pool -->
        <dependency>
            <groupId>commons-pool</groupId>
            <artifactId>commons-pool</artifactId>
            <version>1.6</version>
            <exclusions>
                <exclusion>
                    <artifactId>log4j-slf4j-impl</artifactId>
                    <groupId>*</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>slf4j-log4j12</artifactId>
                    <groupId>*</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>lucene-snowball</artifactId>
            <version>3.0.3</version>
            <scope>compile</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.lucene</groupId>
                    <artifactId>lucene-core</artifactId>
                </exclusion>
                <exclusion>
                    <artifactId>log4j-slf4j-impl</artifactId>
                    <groupId>*</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>slf4j-log4j12</artifactId>
                    <groupId>*</groupId>
                </exclusion>
                <exclusion>
                    <groupId>org.apache.lucene</groupId>
                    <artifactId>lucene-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.hive</groupId>
            <artifactId>hive-jdbc</artifactId>
            <version>2.0.0</version>
            <scope>compile</scope>
            <exclusions>
                <exclusion>
                    <artifactId>log4j-slf4j-impl</artifactId>
                    <groupId>*</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>slf4j-log4j12</artifactId>
                    <groupId>*</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.1-901-1.jdbc4</version>
            <scope>compile</scope>
            <exclusions>
                <exclusion>
                    <artifactId>log4j-slf4j-impl</artifactId>
                    <groupId>*</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>slf4j-log4j12</artifactId>
                    <groupId>*</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-yaml</artifactId>
            <version>2.8.8</version>
            <scope>compile</scope>
            <exclusions>
                <exclusion>
                    <artifactId>log4j-slf4j-impl</artifactId>
                    <groupId>*</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>slf4j-log4j12</artifactId>
                    <groupId>*</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.8.8.1</version>
            <scope>compile</scope>
            <exclusions>
                <exclusion>
                    <artifactId>log4j-slf4j-impl</artifactId>
                    <groupId>*</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>slf4j-log4j12</artifactId>
                    <groupId>*</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.8.2</version>
            <scope>compile</scope>
            <exclusions>
                <exclusion>
                    <artifactId>log4j-slf4j-impl</artifactId>
                    <groupId>*</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>slf4j-log4j12</artifactId>
                    <groupId>*</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.8.2</version>
            <scope>compile</scope>
            <exclusions>
                <exclusion>
                    <artifactId>log4j-slf4j-impl</artifactId>
                    <groupId>*</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>slf4j-log4j12</artifactId>
                    <groupId>*</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.solr</groupId>
            <artifactId>solr-solrj</artifactId>
            <version>5.2.1</version>
            <scope>compile</scope>
            <exclusions>
                <exclusion>
                    <artifactId>log4j-slf4j-impl</artifactId>
                    <groupId>*</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>slf4j-log4j12</artifactId>
                    <groupId>*</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>lucene-analyzers-common</artifactId>
            <version>6.4.0</version>
            <scope>compile</scope>
            <exclusions>
                <exclusion>
                    <artifactId>log4j-slf4j-impl</artifactId>
                    <groupId>*</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>slf4j-log4j12</artifactId>
                    <groupId>*</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.4.6</version>
            <scope>compile</scope>
            <exclusions>
                <exclusion>
                    <artifactId>log4j-slf4j-impl</artifactId>
                    <groupId>*</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>slf4j-log4j12</artifactId>
                    <groupId>*</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.3.4</version>
            <scope>compile</scope>
            <exclusions>
                <exclusion>
                    <artifactId>log4j-slf4j-impl</artifactId>
                    <groupId>*</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>slf4j-log4j12</artifactId>
                    <groupId>*</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-text</artifactId>
            <version>1.0</version>
            <scope>compile</scope>
            <exclusions>
                <exclusion>
                    <artifactId>log4j-slf4j-impl</artifactId>
                    <groupId>*</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>slf4j-log4j12</artifactId>
                    <groupId>*</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <version>1.5.2.RELEASE</version>
            <scope>provided</scope>
            <exclusions>
                <exclusion>
                    <artifactId>log4j-slf4j-impl</artifactId>
                    <groupId>*</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>slf4j-log4j12</artifactId>
                    <groupId>*</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>jdk.tools</groupId>
            <artifactId>jdk.tools</artifactId>
            <version>1.7.0_05</version>
            <scope>system</scope>
            <systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
        </dependency>
    </dependencies>

    <build>
        <finalName>app</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>${jdk.version}</source>
                    <target>${jdk.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <archive>
                        <manifestEntries>
                            <ChangesetNum>${tfs.changeset.num}</ChangesetNum>
                        </manifestEntries>
                    </archive>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>1.5.6.RELEASE</version>
                <configuration>
                    <mainClass>com.rest.api.app</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <properties>
        <jdk.version>1.8</jdk.version>
        <tfs.changeset.num>0</tfs.changeset.num>
    </properties>
</project>

版本冲突的罐子:

路径:&#39; app.war \ WEB-INF \ lib \&#39;

    Gradle                          Maven
slf4j-api-1.7.24.jar            slf4j-api-1.7.5.jar
servlet-api-2.5.jar             servlet-api-2.4.jar
metrics-core-3.1.3.jar          metrics-core-3.1.0.jar
log4j-web-2.7.jar               log4j-web-2.4.1.jar
log4j-1.2-api-2.7.jar           log4j-1.2-api-2.4.1.jar
log4j-1.2.17.jar                log4j-1.2.16.jar
junit-4.12.jar                  junit-4.11.jar
json-20140107.jar               json-20090211.jar
joda-time-2.9.7.jar             joda-time-2.5.jar
jackson-xc-1.9.13.jar           jackson-xc-1.8.3.jar
jackson-jaxrs-1.9.13.jar        jackson-jaxrs-1.8.3.jar
jackson-core-2.8.7.jar          jackson-core-2.8.8.jar
httpmime-4.5.3.jar              httpmime-4.4.1.jar
hadoop-yarn-common-2.6.0.jar    hadoop-yarn-common-2.5.1.jar
hadoop-auth-2.6.0.jar           hadoop-auth-2.5.1.jar
guava-16.0.1.jar                guava-14.0.1.jar
gson-2.8.0.jar                  gson-2.2.4.jar
derby-10.13.1.1.jar             derby-10.10.2.0.jar
commons-logging-1.2.jar         commons-logging-1.1.3.jar
commons-digester-2.1.jar        commons-digester-1.8.jar
commons-collections-3.2.2.jar   commons-collections-3.2.1.jar
commons-codec-1.10.jar          commons-codec-1.6.jar
commons-beanutils-1.9.3.jar     commons-beanutils-1.7.0.jar
classmate-1.3.3.jar             classmate-1.3.1.jar
cglib-2.2.1-v20090111.jar       <Not available> 

问题1:为什么上述罐子的版本不相同?

问题2:为什么使用Gradle构建的战争有&#39; cglib-2.2.1-v20090111.jar&#39;但不是在使用maven构建的战争中?

1 个答案:

答案 0 :(得分:4)

如果依赖关系树中出现多个版本,则Maven和Gradle会遵循不同的规则来选择传递依赖关系的版本。 Maven通常使用最近的外观(来自树根的步数),而Gradle默认使用最高版本

在Maven中,您可以使用DependencyManagement来修复冲突的传递依赖项的版本。