2012-11-27 07:36:28 +08:00
import org.gradle.plugins.ide.eclipse.model.ProjectDependency
2013-01-12 04:31:16 +08:00
import org.gradle.plugins.ide.eclipse.model.SourceFolder
2012-11-27 07:36:28 +08:00
2021-09-15 02:51:59 +08:00
apply plugin: 'eclipse'
2013-01-12 04:31:16 +08:00
2013-05-07 06:21:24 +08:00
eclipse . jdt {
2021-09-22 22:22:27 +08:00
sourceCompatibility = 17
targetCompatibility = 17
2013-05-07 06:21:24 +08:00
}
2013-01-12 04:31:16 +08:00
// Replace classpath entries with project dependencies (GRADLE-1116)
2019-03-06 06:21:36 +08:00
// https://issues.gradle.org/browse/GRADLE-1116
2012-11-27 07:36:28 +08:00
eclipse . classpath . file . whenMerged { classpath - >
2017-08-23 05:16:34 +08:00
def regexp = /.*?\/ ( [ ^ \ /]+)\/ build \ /([^\/ ] + \ /)+(?:main|test)/ // only match those that end in main or test (avoids removing necessary entries like build/classes/jaxb)
2012-12-07 04:56:24 +08:00
def projectOutputDependencies = classpath . entries . findAll { entry - > entry . path = ~ regexp }
projectOutputDependencies . each { entry - >
def matcher = ( entry . path = ~ regexp )
2014-05-30 20:35:01 +08:00
if ( matcher ) {
2012-12-07 04:56:24 +08:00
def projectName = matcher [ 0 ] [ 1 ]
def path = "/${projectName}"
2021-09-15 03:05:57 +08:00
if ( ! classpath . entries . find { e - > e instanceof ProjectDependency & & e . path = = path } ) {
2021-09-15 03:58:22 +08:00
def recursiveDependency = entry . path . matches ( '.+/' + projectName + '/build/([^/]+/)+(?:main|test)' )
2021-09-15 03:05:57 +08:00
// Avoid recursive dependency on current project.
2021-09-15 03:58:22 +08:00
if ( ! recursiveDependency ) {
classpath . entries . add ( new ProjectDependency ( path ) )
2021-09-15 03:05:57 +08:00
}
2012-12-07 04:56:24 +08:00
}
classpath . entries . remove ( entry )
}
}
2021-09-15 03:58:22 +08:00
// Remove any remaining direct depencencies on JARs in the build/libs folder
2022-08-12 17:38:43 +08:00
// except the repack JARs.
2012-12-07 04:56:24 +08:00
classpath . entries . removeAll { entry - > ( entry . path = ~ /(?!.*?repack.*\.jar).*?\/ ( [ ^ \ /]+)\/ build \ /libs\/ [ ^ \ /]+\.jar/ ) }
}
2013-01-12 04:31:16 +08:00
// Use separate main/test outputs (prevents WTP from packaging test classes)
2021-09-15 02:51:59 +08:00
eclipse . classpath . defaultOutputDir = file ( project . name + '/bin/eclipse' )
2013-01-15 02:25:33 +08:00
eclipse . classpath . file . beforeMerged { classpath - >
classpath . entries . findAll { it instanceof SourceFolder } . each {
2021-09-15 02:51:59 +08:00
if ( it . output . startsWith ( 'bin/' ) ) {
2013-01-15 02:25:33 +08:00
it . output = null
}
}
}
2021-09-15 03:42:00 +08:00
eclipse . classpath . file . whenMerged {
entries . findAll { it instanceof SourceFolder } . each {
2021-09-15 02:51:59 +08:00
it . output = 'bin/' + it . path . split ( '/' ) [ 1 ]
2013-01-12 04:31:16 +08:00
}
}
2014-05-30 19:31:02 +08:00
// Ensure project dependencies come after 3rd-party libs (SPR-11836)
// https://jira.spring.io/browse/SPR-11836
2021-09-15 03:42:00 +08:00
eclipse . classpath . file . whenMerged {
entries . findAll { it instanceof ProjectDependency } . each {
2014-05-30 19:31:02 +08:00
// delete from original position
2021-09-15 03:42:00 +08:00
entries . remove ( it )
2014-05-30 19:31:02 +08:00
// append to end of classpath
2021-09-15 03:42:00 +08:00
entries . add ( it )
2014-05-30 19:31:02 +08:00
}
}
2020-11-24 00:24:57 +08:00
// Ensure that JMH sources and resources are treated as test classpath entries
2020-11-24 00:42:49 +08:00
// so that they can see test fixtures.
2020-11-24 00:24:57 +08:00
// https://github.com/melix/jmh-gradle-plugin/issues/157
eclipse . classpath . file . whenMerged {
2022-09-26 20:21:49 +08:00
entries . findAll { it . path = ~ /src\/ jmh \ /(java|kotlin|resources)/ } . each {
2020-11-24 00:24:57 +08:00
it . entryAttributes [ 'test' ] = 'true'
}
}
2013-01-12 04:31:16 +08:00
// Include project specific settings
task eclipseSettings ( type: Copy ) {
from rootProject . files (
2022-06-16 22:13:26 +08:00
'src/eclipse/org.eclipse.core.resources.prefs' ,
2021-09-21 18:37:51 +08:00
'src/eclipse/org.eclipse.jdt.core.prefs' ,
'src/eclipse/org.eclipse.jdt.ui.prefs' )
2021-09-15 02:51:59 +08:00
into project . file ( '.settings/' )
2013-01-15 03:42:12 +08:00
outputs . upToDateWhen { false }
2013-01-12 04:31:16 +08:00
}
2021-09-15 03:42:00 +08:00
task cleanEclipseSettings ( type: Delete ) {
2022-06-16 22:13:26 +08:00
delete project . file ( '.settings/org.eclipse.core.resources.prefs' )
2021-09-15 02:51:59 +08:00
delete project . file ( '.settings/org.eclipse.jdt.core.prefs' )
delete project . file ( '.settings/org.eclipse.jdt.ui.prefs' )
2013-01-12 04:31:16 +08:00
}
2021-09-21 18:37:51 +08:00
tasks [ 'eclipse' ] . dependsOn ( eclipseSettings )
tasks [ 'eclipseJdt' ] . dependsOn ( eclipseSettings )
2021-09-15 03:42:00 +08:00
tasks [ 'cleanEclipse' ] . dependsOn ( cleanEclipseSettings )