首页 > 解决方案 > Gradle 找不到方法编译

问题描述

我第一次尝试使用 gradle。我想要做的是将 ImageJ 与 gradle 一起使用。我从 imageJ 网站获取了示例文件,并从 imageJ github 上的示例中获取了 java 代码。Gradle 说它找不到为 imageJ 编译的方法。

gradle.build 中的代码如下:

buildscript {
    repositories {
        maven {
        url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
//        classpath "io.spring.gradle:dependency-management-plugin:0.5.4.RELEASE"
        classpath "io.spring.gradle:dependency-management-plugin:1.0.8.RELEASE"
    }
}
apply plugin: "io.spring.dependency-management"

repositories {
    jcenter()
    maven {
          url "http://maven.imagej.net/content/groups/public/"
    }
}
dependencyManagement {
    imports {
        mavenBom 'net.imagej:pom-imagej:14.1.0'
    }
}

dependencies {
    compile 'net.imagej:imagej'
}

Java 代码包含在 src/main/java 中,如下所示:

package test;

import java.io.File;

import net.imagej.Dataset;
import net.imagej.ImageJ;

/** Loads and displays a dataset using the ImageJ API. */
public class LoadAndDisplayDataset {

    public static void main(final String... args) throws Exception {
        // create the ImageJ application context with all available services
        final ImageJ ij = new ImageJ();

        // ask the user for a file to open
        final File file = ij.ui().chooseFile(null, "open");

        // load the dataset
        final Dataset dataset = ij.scifio().datasetIO().open(file.getPath());

        // display the dataset
        ij.ui().show(dataset);
    }

}

当我运行时gradle build,我收到以下错误:Could not find method compile() for arguments [net.imagej:imagej

标签: javagradleimagej

解决方案


你知道吗,compile是一个configuration通常由插件引入的;很可能是java插件。在构建脚本之上添加 java 插件应该可以解决问题:

apply plugin: 'java'

因此,您的构建脚本如下所示:

apply plugin: 'java'
apply plugin: "io.spring.dependency-management"

buildscript {
  repositories {
    maven {
      url "https://plugins.gradle.org/m2/"
    }
  }
  dependencies {
    classpath "io.spring.gradle:dependency-management-plugin:1.0.8.RELEASE"
  }
}

repositories {
  jcenter()
  maven {
    url "http://maven.imagej.net/content/groups/public/"
  }
}

dependencyManagement {
  imports {
    mavenBom 'net.imagej:pom-imagej:14.1.0'
  }
}

dependencies {
  compile 'net.imagej:imagej'
}

推荐阅读