Use the 'io.freefair.aspectj' gradle plugin

This commit is contained in:
Lars Grefer 2019-08-05 02:21:03 +02:00 committed by Rob Winch
parent 0410bac559
commit a51318eb95
8 changed files with 17 additions and 151 deletions

View File

@ -1,11 +1,21 @@
apply plugin: 'io.spring.convention.spring-module'
apply plugin: 'aspectj'
apply plugin: 'io.freefair.aspectj'
dependencies {
compile "org.aspectj:aspectjrt"
compile project(':spring-security-core')
compile 'org.springframework:spring-beans'
compile 'org.springframework:spring-context'
compile 'org.springframework:spring-core'
testCompile 'org.springframework:spring-aop'
testAspect sourceSets.main.output
}
sourceSets.main.aspectj.srcDir "src/main/java"
sourceSets.main.java.srcDirs = files()
sourceSets.test.aspectj.srcDir "src/test/java"
sourceSets.test.java.srcDirs = files()
compileAspectj.ajcOptions.outxmlfile = "META-INF/aop.xml"

View File

@ -1,18 +0,0 @@
<?xml version="1.0"?>
<!--
AspectJ load-time weaving config file to install common Spring
aspects.
-->
<aspectj>
<!--
<weaver options="-showWeaveInfo"/>
-->
<aspects>
<aspect
name="org.springframework.security.access.intercept.aspectj.aspect.AnnotationSecurityAspect" />
</aspects>
</aspectj>

View File

@ -3,6 +3,7 @@ buildscript {
classpath 'io.spring.gradle:spring-build-conventions:0.0.23.RELEASE'
classpath "org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion"
classpath 'io.spring.nohttp:nohttp-gradle:0.0.2.RELEASE'
classpath "io.freefair.gradle:aspectj-plugin:3.8.4"
}
repositories {
maven { url 'https://repo.spring.io/plugins-snapshot' }

View File

@ -1,4 +1,4 @@
apply plugin: 'groovy'
apply plugin: 'java'
repositories {
mavenCentral()

View File

@ -1,126 +0,0 @@
package aspectj
import org.gradle.api.Project
import org.gradle.api.Plugin
import org.gradle.api.tasks.TaskAction
import org.gradle.api.logging.LogLevel
import org.gradle.api.file.*
import org.gradle.api.tasks.SourceSet
import org.gradle.api.DefaultTask
import org.gradle.api.GradleException
import org.gradle.api.plugins.JavaPlugin
import org.gradle.api.tasks.compile.JavaCompile
import org.gradle.plugins.ide.eclipse.GenerateEclipseProject
import org.gradle.plugins.ide.eclipse.GenerateEclipseClasspath
import org.gradle.plugins.ide.eclipse.EclipsePlugin
import org.gradle.plugins.ide.eclipse.model.BuildCommand
import org.gradle.plugins.ide.eclipse.model.ProjectDependency
/**
*
* @author Luke Taylor
*/
class AspectJPlugin implements Plugin<Project> {
void apply(Project project) {
project.plugins.apply(JavaPlugin)
if (project.configurations.findByName('ajtools') == null) {
project.configurations.create('ajtools')
project.dependencies {
ajtools "org.aspectj:aspectjtools"
optional "org.aspectj:aspectjrt"
}
}
if (project.configurations.findByName('aspectpath') == null) {
project.configurations.create('aspectpath')
}
project.afterEvaluate {
setupAspectJ(project)
}
}
void setupAspectJ(Project project) {
project.tasks.withType(JavaCompile) { javaCompileTask ->
def javaCompileTaskName = javaCompileTask.name
def ajCompileTask = project.tasks.create(name: javaCompileTaskName + 'Aspect', overwrite: true, description: 'Compiles AspectJ Source', type: Ajc) {
inputs.files(javaCompileTask.inputs.files)
inputs.properties(javaCompileTask.inputs.properties.findAll {it.value != null})
sourceRoots.addAll(project.sourceSets.main.java.srcDirs)
if(javaCompileTaskName.contains("Test")) {
sourceRoots.addAll(project.sourceSets.test.java.srcDirs)
}
compileClasspath = javaCompileTask.classpath
destinationDir = javaCompileTask.destinationDir
aspectPath = project.configurations.aspectpath
}
javaCompileTask.setActions Arrays.asList()
javaCompileTask.dependsOn ajCompileTask
}
project.tasks.withType(GenerateEclipseProject) {
project.eclipse.project.file.whenMerged { p ->
p.natures.add(0, 'org.eclipse.ajdt.ui.ajnature')
p.buildCommands = [new BuildCommand('org.eclipse.ajdt.core.ajbuilder')]
}
}
project.tasks.withType(GenerateEclipseClasspath) {
project.eclipse.classpath.file.whenMerged { classpath ->
def entries = classpath.entries.findAll { it instanceof ProjectDependency}.findAll { entry ->
def projectPath = entry.path.replaceAll('/','')
project.rootProject.allprojects.find{ p->
if(p.plugins.findPlugin(EclipsePlugin)) {
return p.eclipse.project.name == projectPath && p.plugins.findPlugin(AspectJPlugin)
}
false
}
}
entries.each { entry->
entry.entryAttributes.put('org.eclipse.ajdt.aspectpath','org.eclipse.ajdt.aspectpath')
}
}
}
}
}
class Ajc extends DefaultTask {
Set<File> sourceRoots = []
FileCollection compileClasspath
File destinationDir
FileCollection aspectPath
Ajc() {
logging.captureStandardOutput(LogLevel.INFO)
}
@TaskAction
def compile() {
logger.info("="*30)
logger.info("="*30)
logger.info("Running ajc ...")
logger.info("classpath: ${compileClasspath?.files}")
logger.info("srcDirs ${sourceRoots}")
ant.taskdef(resource: "org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties", classpath: project.configurations.ajtools.asPath)
if(sourceRoots.empty) {
return
}
ant.iajc(classpath: compileClasspath.asPath, fork: 'true', destDir: destinationDir.absolutePath,
source: project.convention.plugins.java.sourceCompatibility,
target: project.convention.plugins.java.targetCompatibility,
aspectPath: aspectPath.asPath, sourceRootCopyFilter: '**/*.java', showWeaveInfo: 'true') {
sourceroots {
sourceRoots.each {
logger.info(" sourceRoot $it")
pathelement(location: it.absolutePath)
}
}
}
}
}

View File

@ -1 +0,0 @@
implementation-class=aspectj.AspectJPlugin

View File

@ -1,5 +1,5 @@
apply plugin: 'io.spring.convention.spring-sample'
apply plugin: 'aspectj'
apply plugin: 'io.freefair.aspectj.post-compile-weaving'
repositories {
mavenCentral()
@ -9,7 +9,7 @@ dependencies {
compile project(':spring-security-config')
compile project(':spring-security-core')
aspectpath project(':spring-security-aspects')
aspect project(':spring-security-aspects')
runtime project(':spring-security-aspects')
}

View File

@ -1,10 +1,10 @@
apply plugin: 'io.spring.convention.spring-sample-war'
apply plugin: 'aspectj'
apply plugin: 'io.freefair.aspectj.post-compile-weaving'
dependencies {
compile project(':spring-security-core')
aspectpath project(':spring-security-aspects')
aspect project(':spring-security-aspects')
runtime project(':spring-security-aspects')
runtime project(':spring-security-config')