简体   繁体   English

由于`package ...不存在`,简单的gradle构建失败

[英]Simple gradle build failing due to `package ... does not exist`

A simple beginner project using an external library , that I can't get to build due to something basic I'm missing here.一个使用外部库的简单初学者项目,由于我在这里缺少一些基本的东西,我无法构建它。 Thanks for your help.谢谢你的帮助。

build.gradle构建.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'
    }
  }
}

In ./src/main/java/JavaMotor.java I have import statements:./src/main/java/JavaMotor.java我有导入语句:

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;

all of which are failing on gradle build :所有这些都在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

I'm running Gradle 3.2.1 on Ubuntu.我在 Ubuntu 上运行 Gradle 3.2.1。 IDE is vim and bash. IDE 是 vim 和 bash。

The com.pi4j:pi4j-parent dependency you declared is not a typical dependency you'd expect.您声明的com.pi4j:pi4j-parent依赖项不是您期望的典型依赖项。 It is the parent Maven POM for the Pi4J Project.它是 Pi4J 项目的父级 Maven POM。

If you view the contents of this dependency, you can see there are no published *.jar artifacts: https://repo1.maven.org/maven2/com/pi4j/pi4j-parent/1.2/如果查看此依赖项的内容,可以看到没有发布的*.jar工件: https : //repo1.maven.org/maven2/com/pi4j/pi4j-parent/1.2/

Since there are no published JAR artifacts, you won't have anything on your classpath.由于没有已发布的 JAR 工件,因此您的类路径中不会有任何内容。 What you need to do is declare the dependencies you need:您需要做的是声明您需要的依赖项:

Based on your imports, you'll want to pi4j-core package.根据您的导入,您需要pi4j-core包。 So declare it as a dependency:因此将其声明为依赖项:

dependencies {
    // This dependency is used by the application.
    implementation("com.pi4j:pi4j-core:1.2")
}

The compile configuration is deprecated.不推荐使用compile配置。 See this for more information.有关更多信息,请参阅内容。

That should be enough to fix your imports.这应该足以修复您的进口。

I see you're also redeclaring Java source sets.我看到您也在重新声明 Java 源集。 That is not necessary since the java plugin already does not for you.这不是必需的,因为java插件已经不适合您。 Suggest you familiarize yourself with Gradle: https://docs.gradle.org/current/userguide/getting_started.html建议您熟悉 Gradle: https : //docs.gradle.org/current/userguide/getting_started.html

A full example:一个完整的例子:

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"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM