Skip to content

Instantly share code, notes, and snippets.

@danveloper
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danveloper/4c86a5d262b66ec580df to your computer and use it in GitHub Desktop.
Save danveloper/4c86a5d262b66ec580df to your computer and use it in GitHub Desktop.
import org.gradle.plugins.ide.idea.IdeaPlugin
buildscript {
ext {
springBootVersion = "1.2.0.RELEASE"
}
repositories {
jcenter()
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion"
}
}
apply plugin: 'idea'
apply plugin: 'groovy'
apply plugin: 'spring-boot'
repositories {
jcenter()
}
dependencies {
runtime 'org.springframework:springloaded:1.2.0.RELEASE'
compile 'org.codehaus.groovy:groovy-all:2.4.0'
compile "org.springframework.boot:spring-boot-starter-actuator"
compile "org.springframework.boot:spring-boot-starter-web"
}
// apply the configuration
def ideaPlugin = project == project.rootProject ? project.getPlugins().findPlugin(IdeaPlugin) : project.rootProject.getPlugins().findPlugin(IdeaPlugin)
applyWorkspaceConfig(project.name, project.projectDir.canonicalPath, getSpringloadedJvmArgs(project), "app.Main", ideaPlugin.model.workspace.iws)
void applyWorkspaceConfig(String projectName, String projectDir, String springLoadedJvmArgs, String appClassName, XmlFileContentMerger iws) {
iws.withXml { provider ->
def node = provider.asNode()
def runManagerConfig = node['component'].find { it.'@name' == 'RunManager' } as Node
if (!runManagerConfig) {
runManagerConfig = node.appendNode('component', [name: 'RunManager'])
}
runManagerConfig.append(new XmlParser().parseText("""
<configuration default="false" name="Run ${projectName}" type="Application" factoryName="Application">
<extension name="coverage" enabled="false" merge="false" />
<option name="MAIN_CLASS_NAME" value="${appClassName}" />
<option name="VM_PARAMETERS" value="${springLoadedJvmArgs ? '&quot;' + springLoadedJvmArgs + '&quot; &quot;-noverify&quot;' : ''}" />
<option name="PROGRAM_PARAMETERS" value="" />
<option name="WORKING_DIRECTORY" value="${projectDir}" />
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="false" />
<option name="ALTERNATIVE_JRE_PATH" value="" />
<option name="ENABLE_SWING_INSPECTOR" value="false" />
<option name="ENV_VARIABLES" />
<option name="PASS_PARENT_ENVS" value="true" />
<module name="${projectName}" />
<envs />
<RunnerSettings RunnerId="Debug">
<option name="DEBUG_PORT" value="63810" />
<option name="TRANSPORT" value="0" />
<option name="LOCAL" value="true" />
</RunnerSettings>
<RunnerSettings RunnerId="Run" />
<ConfigurationWrapper RunnerId="Debug" />
<ConfigurationWrapper RunnerId="Run" />
<method />
</configuration>
"""))
}
}
String getSpringloadedJvmArgs(Project project) {
def mainSourceSet = project.convention.getPlugin(JavaPluginConvention).sourceSets.main
def jarFile = JarFinder.find("org.springsource.loaded.SpringLoaded", mainSourceSet.runtimeClasspath.files)
if (jarFile) {
"-javaagent:${jarFile.absolutePath}"
} else {
""
}
}
class JarFinder {
static File find(String className, Collection<File> classpath) {
findJarFile(maybeLoadClass(className, toClassLoader(classpath)))
}
private static File findJarFile(Class<?> targetClass) {
if (targetClass) {
String absolutePath = targetClass.getResource('/' + targetClass.getName().replace(".", "/") + ".class").path
String jarPath = absolutePath.substring("file:".length(), absolutePath.lastIndexOf("!"))
new File(jarPath)
} else {
null
}
}
private static ClassLoader toClassLoader(Collection<File> classpath) {
List<URL> urls = new ArrayList<URL>(classpath.size())
for (File file in classpath) {
try {
urls.add(file.toURI().toURL())
} catch (MalformedURLException ignore) {
}
}
new URLClassLoader(urls as URL[])
}
private static Class<?> maybeLoadClass(String className, ClassLoader classLoader) {
if (classLoader) {
try {
return classLoader.loadClass(className)
} catch (ClassNotFoundException ignore) {
}
}
null
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment