简体   繁体   中英

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

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:

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 :

: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. IDE is vim and bash.

The com.pi4j:pi4j-parent dependency you declared is not a typical dependency you'd expect. It is the parent Maven POM for the Pi4J Project.

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/

Since there are no published JAR artifacts, you won't have anything on your classpath. What you need to do is declare the dependencies you need:

Based on your imports, you'll want to pi4j-core package. 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. See this for more information.

That should be enough to fix your imports.

I see you're also redeclaring Java source sets. That is not necessary since the java plugin already does not for you. Suggest you familiarize yourself with 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"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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