eclipse / buildship

The Eclipse Plug-ins for Gradle project.
530 stars 168 forks source link

Support for runtimeOnly dependency #1135

Open nbauma109 opened 2 years ago

nbauma109 commented 2 years ago

With gradle it's now possible to use a runtimeOnly dependency

When I set a dependency as runtimeOnly in the build.gradle, I expect it to be unavailable to compile, but it's still included in the compile classpath.

The expected behaviour with the following example is a compilation error.

See in the screenshot below the illustration of the difference of behaviour between maven (m2e) and gradle in Eclipse.

image

build.gradle :

plugins {
  id 'java'
}

repositories {
  mavenCentral()
}

dependencies {
    runtimeOnly 'commons-io:commons-io:2.11.0'
}

pom.xml :

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany.myproject</groupId>
    <artifactId>test-maven</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.11.0</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
</project>

In src/main/java, Main.java :

import org.apache.commons.io.IOUtils;

public class Main {
}
donat commented 2 years ago

This is another example of the flaws of how Buildship translates Gradle dependency information to Eclipse. In a nutshell, you get a union of all dependencies from eclipse.classpath.plusConfigurations. We are aware that it needs more fine-tuning. You can work around the issue by removing commos-io from the compile classpath in a eclipse.classpath.file.whenMerged block.

I'm closing this issue to keep the board clean, but it doesn't mean that we don't recognize the problem. I'll look into what can be done here in the future.

dovogt commented 6 months ago

Unfortunately, the workaround does not work. In your example, there are problems with the execution via Eclipse, as the "commons-io" is required for runtime. Eclipse itself only recognises one scope and always uses this (runtime, api, implementation). I have managed without Gradle by restricting access to the library itself. Unfortunately, this is not possible via the Projectdependencies Container. Maybe you can start there.