Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forbiddenapis plugin sometimes does not load correctly with Gradle plugin mechanism #144

Closed
uschindler opened this issue Sep 2, 2018 · 3 comments
Assignees
Labels
Milestone

Comments

@uschindler
Copy link
Member

Depending on what plugins and in which order you add to gradles plugins { ... } block in the build file, forbiddenapis does not load. Using the currently documented way it works, but since the plugin was released to the Gradle Plugin Portal (see #107) this does not work:

build.gradle

plugins {
  id 'java'
  id 'de.thetaphi.forbiddenapis' version '2.5'
}

repositories {
  jcenter()
}

group = 'test'
version = '1.0.0'

sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
* Where:
Build file '/Users/dev/tmp/forbiddenapis/build.gradle' line: 3

* What went wrong:
An exception occurred applying plugin request [id: 'de.thetaphi.forbiddenapis', version: '2.5']
> Failed to apply plugin [id 'de.thetaphi.forbiddenapis']
   > Cannot execute Groovy script for apply(Project).
      > Forbidden-apis only works in projects using the java plugin.

The problem is that plugin-init.groovy check, if the JavaBasePlugin was loaded before the forbidden plugin was applied. With the new plugin mechanism it's not guaranteed.

To fix this, we change the init script to just apply the JavaBasePlugin on startup. Newer gradle plugins do this in the same way.

Unfortunately we have to release a new version. Thanks @sdavids for reporting.

@uschindler
Copy link
Member Author

This is the proposed fix:

diff --git a/src/main/resources/de/thetaphi/forbiddenapis/gradle/plugin-init.groovy b/src/main/resources/de/thetaphi/forbiddenapis/gradle/plugin-init.groovy
index 1b0f9c4..a7f14b0 100644
--- a/src/main/resources/de/thetaphi/forbiddenapis/gradle/plugin-init.groovy
+++ b/src/main/resources/de/thetaphi/forbiddenapis/gradle/plugin-init.groovy
@@ -18,11 +18,8 @@
 
 import java.lang.reflect.Modifier;
 import org.gradle.api.plugins.JavaBasePlugin;
-import org.gradle.api.plugins.PluginInstantiationException;
 
-if (project.plugins.withType(JavaBasePlugin.class).isEmpty()) {
-  throw new PluginInstantiationException('Forbidden-apis only works in projects using the java plugin.');
-}
+project.plugins.apply(JavaBasePlugin.class);
 
 // check if running in Gradle Daemon?
 // see: http://stackoverflow.com/questions/23265217/how-to-know-whether-you-are-running-inside-a-gradle-daemon

@uschindler
Copy link
Member Author

It's fixed. The fix comes with next version.

@uschindler uschindler self-assigned this Sep 2, 2018
@uschindler uschindler added this to the 2.6 milestone Sep 2, 2018
@uschindler uschindler added the bug label Sep 2, 2018
@uschindler
Copy link
Member Author

uschindler commented Sep 2, 2018

@sdavids: I was about to release a 2.5.1 bugfix release, but this fix alone does not help. To work correctly, also the fix for #138 is needed, otherwise it would not apply the tasks for all sourcesets (the "java-base" plugin does not add sourcesets, so the order is important, too).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

1 participant