2016-07-06 305 views
1

我正在编辑一个使用gradle进行构建/依赖管理的开源包。我以前没有用过gradle。导入gradle依赖关系

我想使用一些杰克逊方法,所以我将compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.7.5'加到build.gradle文件中,并将import com.fasterxml.jackson.*;加到我的项目中。

但是当我尝试建立与./gradle shadowJar,我得到以下错误:

Installing pre-commit hook for Unix/OS X 
WARNING: jar does not create an executable jar. Use shadowJar instead. 
:compileJava 
/Users/mttjone/sw/builds/bixie-vanilla/src/main/java/bixie/checker/reportprinter/JSONReportPrinter.java:11: error: package com.fasterxml.jackson does not exist 
import com.fasterxml.jackson.*; 
^ 

... 

4 errors 
:compileJava FAILED 

FAILURE: Build failed with an exception. 

* What went wrong: 
Execution failed for task ':compileJava'. 
> Compilation failed; see the compiler error output for details. 

* Try: 
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. 

BUILD FAILED 

应广大用户要求:

if(JavaVersion.current() < JavaVersion.VERSION_1_7){ 
    println("\t************************") 
    println("\t*** Hello from Bixie ***") 
    println("\tYou will need Java 1.7 or higher if you want to continue.") 
    println("\tYour Java is really old. Found version " + JavaVersion.current()) 
    println("\t************************") 
    throw new GradleException("Update your Java!")  
} 

import org.apache.tools.ant.taskdefs.condition.Os 

//install the commit hook if possible. 
def hook_folder = new File('./.git/hooks') 
def hook = new File('pre-commit.sh') 
def installed_hook = new File('./.git/hooks/pre-commit') 

if (Os.isFamily(Os.FAMILY_WINDOWS)) { 
println("Installing pre-commit hook for WINDOWS") 
if (hook.exists() && hook_folder.exists() && !installed_hook.exists()) { 
    exec { 
    commandLine 'cmd', '/c', 'copy', hook.getAbsolutePath(), installed_hook.getAbsolutePath() 
    } 
} 
} else { 
println("Installing pre-commit hook for Unix/OS X") 
if (hook.exists() && hook_folder.exists() && !installed_hook.exists()) { 
    exec { 
    workingDir '.' 
    commandLine 'cp', hook, installed_hook 
    } 
} 
} 


apply plugin: 'java' 
apply plugin: 'eclipse-wtp' 
apply plugin: 'idea' 
apply plugin: 'jacoco' 
apply plugin: 'com.github.kt3k.coveralls' 
apply plugin: 'com.github.johnrengelman.shadow' 
apply plugin: 'application' 
apply plugin: 'findbugs' 
//apply plugin: 'jdepend' 


apply plugin: 'checkstyle' 

task wrapper(type: Wrapper) { 
    gradleVersion = '2.3' 
} 


def version = '1.0' 
jar.archiveName = "bixie_dyn.jar" 
shadowJar.archiveName = "bixie.jar" 
mainClassName = "bixie.Main" 

repositories { 
    mavenCentral() 
} 

buildscript { 
    repositories { 
     mavenCentral() 
     maven { 
      name 'Shadow' 
      url "https://plugins.gradle.org/m2/" 
     } 
    } 

    dependencies { 
     classpath 'org.kt3k.gradle.plugin:coveralls-gradle-plugin:2.0.1' 
     classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.2' 
    } 
} 

configurations{ 
    common 
} 

dependencies { 
    compile 'args4j:args4j:2.32'  
    compile 'log4j:log4j:1.2.17' 
    compile 'org.scala-lang:scala-actors:2.11.7' 
    compile 'org.scala-lang:scala-library:2.11.7' 
    compile 'com.googlecode.json-simple:json-simple:1.1.1' 
    compile 'net.sourceforge.findbugs:annotations:1.3.2' 
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.7.5' 

    compile fileTree(dir: 'lib', include: '*.jar')  
    testCompile "junit:junit:4.11" // Or whatever version 
} 


// building the jar --------------------- 

jar { 
    println("WARNING: jar does not create an executable jar. Use shadowJar instead.") 
    baseName = 'bixie' 

    from configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } 

    from('src/main/resources'){ include('log4j.properties')} 
    from('src/main/resources'){ include('basic_prelude.bpl')} 
    from('src/main/resources'){ include('java_lang.bpl')} 



    manifest { 
     attributes 'Main-Class': mainClassName, 
        'Class-Path': '.', 
        'Implementation-Title': 'Bixie', 
        'Implementation-Version': version 
    } 
} 

shadowJar { 
    append('src/main/resources/report_html.zip') 
} 


//jar.dependsOn shadowJar 

// testing related activities ----------------- 
tasks.withType(FindBugs) { 
    effort = "max" 
    reportLevel = "medium" 

    findbugs.excludeFilter = file("$rootProject.projectDir/config/findbugs/excludeFilter.xml") 

    reports { 
     xml.enabled = false 
     html.enabled = true 
    } 
} 



jacocoTestReport { 
    reports { 
     xml.enabled true 
     html.enabled true 
     csv.enabled false 
     html.destination "${buildDir}/reports/coverage" 
    } 
} 

test { 
    jacoco { 
    enabled = true 
    } 

    testLogging { 
     events "failed" 
     exceptionFormat "full" 
    } 

    useJUnit() 
} 

task selfCheck { 
    group 'Verification' 
    description 'Run Bixie on itself.' 

    doLast { 
    def bixieJar = shadowJar.archivePath 
    def bixieDir = compileJava.destinationDir 
    def bixieClassPath = compileJava.classpath.asPath 
    //TODO generate this. 
    def bixieReportDir = "${buildDir}/reports/self_test.txt" 

    exec { 
     workingDir '.' 
     commandLine 'java', '-jar', bixieJar, '-j', bixieDir, '-cp', bixieClassPath, '-o', bixieReportDir, '-checker', '3' 
    } 

    } 
} 

task inferCheck { 
    group 'Verification' 
    description 'Run Facebook Infer on this project.' 

    doLast { 
    println("REQUIRES Infer TO BE IN YOUR PATH.") 

    exec { 
     workingDir '.' 
     commandLine 'infer', '--', 'gradle', 'clean', 'compileJava' 
    } 

    } 
} 

gradle这个依赖的相关位:

runtime - Runtime classpath for source set 'main'. 
+--- args4j:args4j:2.32 
+--- log4j:log4j:1.2.17 
+--- org.scala-lang:scala-actors:2.11.7 
| \--- org.scala-lang:scala-library:2.11.7 
+--- org.scala-lang:scala-library:2.11.7 
+--- com.googlecode.json-simple:json-simple:1.1.1 
| \--- junit:junit:4.10 
|   \--- org.hamcrest:hamcrest-core:1.1 
+--- net.sourceforge.findbugs:annotations:1.3.2 
\--- com.fasterxml.jackson.core:jackson-databind:2.7.5 
    +--- com.fasterxml.jackson.core:jackson-annotations:2.7.0 
    \--- com.fasterxml.jackson.core:jackson-core:2.7.5 

回答

1

我认为你的问题与Gradle无关,只是纠正你的代码。

com.fasterxml.jackson真的存在吗?
请注意,只是包com.fasterxml.jackson.databindcom.fasterxml.jackson.core存在说什么关于包com.fasterxml.jackson。这三个软件包是绝对不相关和独立的。没有像“小包装”这样的东西。