-
Notifications
You must be signed in to change notification settings - Fork 34
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
Add support for Gradle #68
Comments
I added a new branch: https://github.com/policeman-tools/forbidden-apis/compare/issue68 |
Looking good, thank you! Could you possibly make the plugin work with all sourceSets? This would allow eg. adding a new testInteg which is also checked? |
Hi @rjernst, In that case you would need to configure the task on your own, which should work with plain gradle/groovy. The class is there, you just have to define 2 things: classesDir + classpath. By the way, the current version that was committed to the branch works correct now. I have several Gradle projects from the examples shipped with gradle extended by this plugin, works. Are there any plans to switch Elasticsearch builds to Gradle? |
This is an example (modify Gradle's samples/java/quickstart example): apply plugin: 'java'
buildscript {
repositories {
mavenLocal()
}
dependencies {
classpath 'de.thetaphi:forbiddenapis:1.9-SNAPSHOT'
}
}
apply plugin: 'de.thetaphi.forbiddenapis'
sourceCompatibility = 1.5
version = '1.0'
jar {
manifest {
attributes 'Implementation-Title': 'Gradle Quickstart',
'Implementation-Version': version
}
}
repositories {
mavenCentral()
}
dependencies {
compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
testCompile group: 'junit', name: 'junit', version: '4.+'
}
test {
systemProperties 'property': 'value'
}
forbiddenApis {
signatures = ['java.lang.String']
bundledSignatures = ['jdk-unsafe','jdk-deprecated']
failOnViolation = false
}
testForbiddenApis {
signatures = ['org.gradle.Person']
bundledSignatures = ['jdk-unsafe','jdk-deprecated']
failOnViolation = false
}
uploadArchives {
repositories {
flatDir {
dirs 'repos'
}
}
} |
I changed the task dependencies in the branch a bit (after sleeping about it): I am currently investigating why the forbidden-checks run all the time although there are no changes. I tried to add outpus == inputs, but this did not help. |
I refactored the task now, so inputs/outputs work correctly. The issue was that annotation don't work correctly on fields without getters. I also changed the task to be PatternFilterable, so it now behaves similar to other tasks taking input files, |
Why not just install the tasks and let he user decide what should depend on them? It is really simple for the user to eg add it to check:
In my particular case, I want to add a |
Hi,
What do you think? |
FYI: PMD says in https://docs.gradle.org/current/userguide/pmd_plugin.html: "The plugin adds a number of tasks to the project that perform the quality checks. You can execute the checks by running |
Hmm... Maven's validate is executed before classes are compiled, Uwe -- this means you'd be verifying last run's output. Does it make sense? |
Maybe I mixed up the name. I mean the phase after test. I think it's "verify". Both just start with "v". |
You mean 'verify'. I personally think the way it was bound (process-classes) makes a lot of sense -- an early warning signal rather than delayed check. |
Most projects I have seen changed this to verify. Also Elasticsearch did it very late for long time. |
Well, if "majority is right" is the argument then sure... :) Note that 'verify' is executed after packaging to you can build the package with invalid method calls. To me this is a no-no. But people can change the binding anyway if they like, so up to you. |
But "install" or "deploy" requires to run "verify"... So you cannot publish in Maven Central without that passing (yes you can copy the JAR files away from target/ folder...). In any case, a later stage also helps Gradle. If you bind it as dependency for "check", it is easy for anybody to just add another dependency. Removing the current dependency on "classes"/"testClasses" is almost impossible in "clean & nice groovy code". |
@uschindler The gradle plan sounds good to me. Maven I know nothing about, it is black magic. |
Yes, but if you're using maven and wish to stay sane you wouldn't install to a local repo, even for deployment (you'd redirect the repo to a temp. dir). Anyway, it's just a different opinion, I don't care either way. |
Now I can maybe do this. When the plugin installs itsself it would register a task for every sourceSet, for "main" and "test" it would depend on the corresponding compile tasks, but not for others. PMD/CheckStyle does the same, very cool: https://docs.gradle.org/current/userguide/pmd_plugin.html; https://docs.gradle.org/current/userguide/checkstyle_plugin.html |
@uschindler In terms of @rjernst's request, you can depend on the I tried to setup the dependencies so that it would short circuit a build if the Forbidden APIs were used (by making You can also have the task marked with This looks like great progress to me. |
Without upgrading to Java 1.6 this is impossible (because I have to compile against Gradle 1.x if I want to stay on 1.5 for the plugin). See my comment above. For 2.0 I would drop Java 5. |
Thats already in the process of changing - I noticed that, too :-) |
@uschindler Is there any reason to maintain support for even Java 6? I chose to support Java 7 as a minimum because I know a lot of people still use it, but Java 6 has not been supported for years. I'm certainly only referring to 2.0 of the plugin. |
No need for Java 7, so I stay with Java 6. You cannot use NIO.2 anyways because thats incompatible with any build tool outside. |
I started to refactor plugin initialization. Don't ask why I did it that way, but that simplifies things enormous... |
Another small suggestion: set the |
Or maybe it doesn't matter. I was concerned it would show up in the "Other" category when running |
I implemented now the task for all source sets. This now works using same logic like the other check tasks (PMD, CheckStyle). // initializes the plugin and binds it to the lifecycle
import org.gradle.api.plugins.PluginInstantiationException;
import org.gradle.api.tasks.TaskContainer;
if (!project.plugins.findPlugin("java")) {
throw new PluginInstantiationException("Forbiddenapis only works in projects using the 'java' plugin.");
}
def tasks = project.getTasks();
def checkTask = tasks.getByName("check");
// Define our tasks (one for each SourceSet):
def forbiddenTasks = project.sourceSets.collect { sourceSet ->
tasks.create(sourceSet.getTaskName(FORBIDDEN_APIS_TASK_NAME_PREFIX, null), CheckForbiddenApis.class) { task ->
task.classesDir = sourceSet.output.classesDir;
task.classpath = sourceSet.compileClasspath;
task.description = "Runs forbiddenApis checks on '" + sourceSet.name + "' classes.";
// task.group = checkTask.group;
task.dependsOn(sourceSet.output);
}
}
// Add our tasks as dependencies to chain
checkTask.dependsOn(forbiddenTasks); I commented out the task's group for now. Adding it would make it show up next to "check". The output now is:
Please note, the task name changed a bit, the new one is automatically generated from the SourceSet method. |
I did some testing already: With the current code it is perfectly possible to declare own sourceSets, but you have to do this before loading the Plugin:
If you load the plugin before, it would not create the tasks, because the sourceSets are not there at time of plugin loading. I chcked this: It behaves exactly the same for Checkstyle and PMD and all other check plugins (they all chare the same superclass). The only thing that is missing currently is a ForbiddenApisExtension that can be used to define defaults. This extension looks like a task ("forbiddenApis") in the DSL, but its settings are just copied over into the task implementations. So you can define the bundledSignatures. Without the extension, the above build script would fail, because you also have to add the task settings for the new (automatically defined) "forbiddenApisDummy" task. I will look into this, it works with a mechanism called "ConventionMappings". With the Extension, it may be needed to remove the "convenience" task "forbiddenApis" because this would create naming conflict. Alternatively call the extension "forbiddenApisDefaults". |
I implemented extension:
The forbiddenApis closure refers to the extension and defines all defaults. The tasks inherit those. In the test task, you see that it is possible to add further signatures using standard groovy syntax (adding stuff to lists). |
I also tested recently that the plugin also works if you have a groovy-only project. If you setup a Groovy project and one of your source code files (implemented in Groovy) uses forbidden apis (after compilation with Groovyc) it also fails :-)
|
I added a pull request as all main features are working now: #70 |
The Elasticsearch people started with https://github.com/elastic/forbidden-apis-gradle-plugin.
I think this should be included directly into the forbidden-apis tool. Basically this is more or less the Maven tool, just implemented as Gradle's DefaultTask extension. This can be implemented in pure Java, this is what I would prefer.
As usual all would be packaged in the same JAR file because there are no conflicts between the tools.
The text was updated successfully, but these errors were encountered: