一个简单的初学者项目,使用external library,由于我在这里缺少一些基本的东西而无法构建。谢谢你的帮助。

build.gradle

plugins {
  id 'java'
  id 'maven'
}

repositories {
  mavenCentral()
}

dependencies {
  compile 'com.pi4j:pi4j-parent:1.2'
}

sourceSets {
  main {
    java {
      srcDir 'src'
    }
  }
  test {
    java {
      srcDir 'test'
    }
  }
}

./src/main/java/JavaMotor.java中,我有import语句:
import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.PinState;
import com.pi4j.io.gpio.RaspiPin;

所有这些都在gradle build上失败:
:compileJava/home/pi/workspaces/javamotor/src/main/java/JavaMotor.java:1: error: package com.pi4j.io.gpio does not exist
import com.pi4j.io.gpio.GpioController;
                       ^
/home/pi/workspaces/javamotor/src/main/java/JavaMotor.java:2: error: package com.pi4j.io.gpio does not exist
import com.pi4j.io.gpio.GpioFactory;
                       ^
/home/pi/workspaces/javamotor/src/main/java/JavaMotor.java:3: error: package com.pi4j.io.gpio does not exist
import com.pi4j.io.gpio.GpioPinDigitalOutput;
                       ^
/home/pi/workspaces/javamotor/src/main/java/JavaMotor.java:4: error: package com.pi4j.io.gpio does not exist
import com.pi4j.io.gpio.PinState;
                       ^
/home/pi/workspaces/javamotor/src/main/java/JavaMotor.java:5: error: package com.pi4j.io.gpio does not exist
import com.pi4j.io.gpio.RaspiPin;
                       ^
5 errors
 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

Total time: 3.107 secs

我在Ubuntu上运行Gradle 3.2.1。 IDE是vim和bash。

最佳答案

您声明的com.pi4j:pi4j-parent依赖项不是您期望的典型依赖项。它是Pi4J项目的父Maven POM。

如果查看此依赖项的内容,则可以看到没有发布的*.jar Artifact :https://repo1.maven.org/maven2/com/pi4j/pi4j-parent/1.2/

由于没有发布的JAR Artifact ,因此您的类路径中将没有任何内容。您需要做的是声明所需的依赖项:

  • https://github.com/Pi4J/pi4j
  • https://mvnrepository.com/artifact/com.pi4j

  • 根据您的导入,您需要pi4j-core包。因此,将其声明为依赖项:
    dependencies {
        // This dependency is used by the application.
        implementation("com.pi4j:pi4j-core:1.2")
    }
    



    那应该足以解决您的进口问题。

    我看到您也在重新声明Java源代码集。这是不必要的,因为java插件已经不适合您。建议您熟悉Gradle:https://docs.gradle.org/current/userguide/getting_started.html

    一个完整的例子:
    plugins {
        // Apply the application plugin to add support for building a CLI application.
        // The application plugin implicitly applies the Java plugin
        id("application")
    }
    
    repositories {
        // Use central for resolving dependencies.
        // You can declare any Maven/Ivy/file repository here.
        mavenCentral()
    }
    
    dependencies {
        // This dependency is used by the application.
        implementation("com.pi4j:pi4j-parent:1.2")
    }
    
    application {
        // Define the main class for the application.
        mainClassName = "io.mateo.MyApplication"
    }
    
  • https://docs.gradle.org/current/userguide/application_plugin.html
  • 07-24 19:05