使用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。
答案 0 :(得分:1)
您声明的com.pi4j:pi4j-parent
依赖项不是您期望的典型依赖项。它是Pi4J项目的父级Maven POM。
如果查看此依赖项的内容,则可以看到没有发布的*.jar
工件:https://repo1.maven.org/maven2/com/pi4j/pi4j-parent/1.2/
由于没有发布的JAR工件,因此您的类路径中没有任何内容。您需要做的是声明所需的依赖项:
根据您的导入,您需要pi4j-core
打包。因此,将其声明为依赖项:
dependencies {
// This dependency is used by the application.
implementation("com.pi4j:pi4j-core:1.2")
}
不推荐使用
compile
配置。有关更多信息,请参见this。
那足以解决您的进口问题。
我看到您也在重新声明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"
}