0

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.

Steven
  • 1,049
  • 2
  • 14
  • 32
  • Sorry, learning gradle. Does `compile` not use the dependency? If not, what should I declare instead? Thanks. – Steven Jan 11 '20 at 23:43

1 Answers1

1

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"
}
Cisco
  • 20,972
  • 5
  • 38
  • 60
  • Thanks, this worked for me, however I'm on gradle 3.2.1 so I kept the `compile` configuration (I believe `implementation` is from 3.4 on). – Steven Jan 12 '20 at 00:33
  • Suggest you upgrade if possible https://docs.gradle.org/current/userguide/gradle_wrapper.html#sec:upgrading_wrapper – Cisco Jan 12 '20 at 01:13