|
| 1 | +// Build.gradle for creating or installing new instrumentation modules |
| 2 | + |
| 3 | + |
| 4 | +// Global defaults - override here or in individual modules as needed. |
| 5 | +buildscript { |
| 6 | + repositories { |
| 7 | + flatDir dirs: 'template-lib' |
| 8 | + mavenLocal() |
| 9 | + mavenCentral() |
| 10 | + gradlePluginPortal() |
| 11 | + } |
| 12 | + |
| 13 | + dependencies { |
| 14 | + classpath 'gradle-templates:gradle-templates:1.5' |
| 15 | + classpath 'com.newrelic.agent.java:gradle-verify-instrumentation-plugin:3.2' |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +plugins { |
| 20 | + id "de.undercouch.download" version "1.2" |
| 21 | +} |
| 22 | + |
| 23 | +project.ext { |
| 24 | + group = 'com.newrelic.instrumentation' |
| 25 | + javaAgentVersion = '6.4.0' |
| 26 | + |
| 27 | + // Aligned with minimum Java major version supported by latest Java Agent |
| 28 | + javaVersion = JavaVersion.VERSION_1_8 |
| 29 | + |
| 30 | +} |
| 31 | + |
| 32 | +apply plugin: 'java' |
| 33 | +apply plugin: 'de.undercouch.download' |
| 34 | + |
| 35 | +import templates.* |
| 36 | +import de.undercouch.gradle.tasks.download.Download |
| 37 | + |
| 38 | +task getAgent(type: Download) { |
| 39 | + def rootProject = projectDir.path |
| 40 | + src 'https://repo1.maven.org/maven2/com/newrelic/agent/java/newrelic-agent/'+project.javaAgentVersion+'/newrelic-agent-'+project.javaAgentVersion+'.jar' |
| 41 | + dest projectDir.path+"/libs/newrelic-agent-"+project.javaAgentVersion+".jar" |
| 42 | +} |
| 43 | + |
| 44 | +task extractJars(type: Copy) { |
| 45 | + |
| 46 | + from zipTree(projectDir.path+"/libs/newrelic-agent-"+project.javaAgentVersion+".jar") |
| 47 | + into projectDir.path+"/libs" |
| 48 | +} |
| 49 | + |
| 50 | +task cleanUp(type: Delete) { |
| 51 | + delete projectDir.path+'/libs/META-INF', projectDir.path+'/libs/com', projectDir.path+'/libs/mozilla' |
| 52 | + delete projectDir.path+'/libs/LICENSE', projectDir.path+'/libs/Log4j-events.dtd', projectDir.path+'/libs/THIRD_PARTY_NOTICES.md' |
| 53 | + delete fileTree(projectDir.path+'/libs') { |
| 54 | + include '**/*.xsd' |
| 55 | + include '**/*.xml' |
| 56 | + include '**/*.yml' |
| 57 | + include '**/*.properties' |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +task checkForDependencies(type: Exec) { |
| 62 | + environment "JAVAAGENTVERSION", javaAgentVersion |
| 63 | + def rootProject = projectDir.path |
| 64 | + def cmdLine = rootProject+'/newrelic-dependencies.sh' |
| 65 | + workingDir rootProject |
| 66 | + commandLine cmdLine |
| 67 | + |
| 68 | +} |
| 69 | + |
| 70 | +task buildIfNeeded { |
| 71 | + dependsOn checkForDependencies |
| 72 | + dependsOn jar |
| 73 | + tasks.findByName('jar').mustRunAfter 'checkForDependencies' |
| 74 | +} |
| 75 | + |
| 76 | +task createModule { |
| 77 | + dependsOn checkForDependencies |
| 78 | + description = 'Generate project files for a new instrumentation module' |
| 79 | + group = 'New Relic' |
| 80 | + doLast { |
| 81 | + |
| 82 | + def rootProject = projectDir.path |
| 83 | + |
| 84 | + String projectGroup = TemplatesPlugin.prompt('Instrumentation Module Group (default: ' + project.ext.group + ') (Hit return to use default):\n') |
| 85 | + String projectName = TemplatesPlugin.prompt('Instrumentation Module Name:\n') |
| 86 | + |
| 87 | + if (projectName == null) { |
| 88 | + throw new Exception("Please specify a valid module name.") |
| 89 | + } else { |
| 90 | + projectName = projectName.trim() |
| 91 | + } |
| 92 | + |
| 93 | + if (projectGroup == null || projectGroup.trim() == '') { |
| 94 | + projectGroup = project.ext.group |
| 95 | + } else { |
| 96 | + projectGroup = projectGroup.trim() |
| 97 | + } |
| 98 | + |
| 99 | + def projectLibDir = new File(rootProject+'/lib') |
| 100 | + |
| 101 | + def projectPath = new File(projectName) |
| 102 | + if (projectPath.exists()) { |
| 103 | + throw new Exception(projectPath.path + ' already exists.') |
| 104 | + } |
| 105 | + |
| 106 | + def projectJava = new File(projectPath, 'src/main/java') |
| 107 | + def projectTest = new File(projectPath, 'src/test/java') |
| 108 | + mkdir projectJava |
| 109 | + mkdir projectTest |
| 110 | + |
| 111 | + ProjectTemplate.fromRoot(projectPath) { |
| 112 | + 'build.gradle' ''' |
| 113 | + // Build.gradle generated for instrumentation module PROJECT_NAME |
| 114 | +
|
| 115 | + apply plugin: 'java' |
| 116 | +
|
| 117 | + dependencies { |
| 118 | + // Declare a dependency on each JAR you want to instrument |
| 119 | + // Example: |
| 120 | + // implementation 'javax.servlet:servlet-api:2.5' |
| 121 | +
|
| 122 | + // New Relic Java Agent dependencies |
| 123 | + implementation 'com.newrelic.agent.java:newrelic-agent:JAVA_AGENT_VERSION' |
| 124 | + implementation 'com.newrelic.agent.java:newrelic-api:JAVA_AGENT_VERSION' |
| 125 | + implementation fileTree(include: ['*.jar'], dir: '../libs') |
| 126 | + implementation fileTree(include: ['*.jar'], dir: '../test-lib') |
| 127 | + } |
| 128 | +
|
| 129 | + jar { |
| 130 | + manifest { |
| 131 | + attributes 'Implementation-Title': 'PROJECT_GROUP.PROJECT_NAME' |
| 132 | + attributes 'Implementation-Vendor': 'New Relic' |
| 133 | + attributes 'Implementation-Vendor-Id': 'com.newrelic' |
| 134 | + attributes 'Implementation-Version': 1.0 |
| 135 | + } |
| 136 | + } |
| 137 | +
|
| 138 | + verifyInstrumentation { |
| 139 | + // Verifier plugin documentation: |
| 140 | + // https://github.com/newrelic/newrelic-gradle-verify-instrumentation |
| 141 | + // Example: |
| 142 | + // passes 'javax.servlet:servlet-api:[2.2,2.5]' |
| 143 | + // exclude 'javax.servlet:servlet-api:2.4.public_draft' |
| 144 | + }'''.replace('PROJECT_GROUP', projectGroup) |
| 145 | + .replace('PROJECT_NAME', projectName) |
| 146 | + .replace('PROJECT_PATH', projectPath.path) |
| 147 | + .replace('JAVA_AGENT_VERSION', project.ext.javaAgentVersion) |
| 148 | + } |
| 149 | + |
| 150 | + File settings = new File('settings.gradle') |
| 151 | + settings.append("include '$projectName'\n") |
| 152 | + println "Created module in $projectPath.path." |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +subprojects { |
| 157 | + repositories { |
| 158 | + mavenLocal() |
| 159 | + mavenCentral() |
| 160 | + } |
| 161 | + |
| 162 | + apply plugin: 'java' |
| 163 | + apply plugin: 'eclipse' |
| 164 | + apply plugin: 'idea' |
| 165 | + apply plugin: 'com.newrelic.gradle-verify-instrumentation-plugin' |
| 166 | + |
| 167 | + sourceCompatibility = project.javaVersion |
| 168 | + targetCompatibility = project.javaVersion |
| 169 | + |
| 170 | + dependencies { |
| 171 | + testImplementation fileTree(dir: '../lib', include: "*.jar") // + project.javaAgentVersion |
| 172 | + testImplementation 'org.nanohttpd:nanohttpd:2.3.1' |
| 173 | + testImplementation 'com.newrelic.agent.java:newrelic-agent:' + project.javaAgentVersion |
| 174 | + } |
| 175 | + |
| 176 | + task install(dependsOn: buildIfNeeded, type: Copy) { |
| 177 | + description = 'Copies compiled jar to the NEW_RELIC_EXTENSIONS_DIR.' |
| 178 | + group = 'New Relic' |
| 179 | + |
| 180 | + def extDir = System.getenv("NEW_RELIC_EXTENSIONS_DIR") ?: " " |
| 181 | + |
| 182 | + from jar |
| 183 | + into extDir |
| 184 | + } |
| 185 | + |
| 186 | + compileJava.doFirst { |
| 187 | + tasks.findByName('checkForDependencies') |
| 188 | + } |
| 189 | + |
| 190 | + install.doFirst { |
| 191 | + def extDir = System.getenv("NEW_RELIC_EXTENSIONS_DIR") |
| 192 | + if (extDir == null) { |
| 193 | + throw new Exception("Must set NEW_RELIC_EXTENSIONS_DIR.") |
| 194 | + } |
| 195 | + |
| 196 | + if (extDir.startsWith("~" + File.separator)) { |
| 197 | + extDir = System.getProperty("user.home") + extDir.substring(1); |
| 198 | + } |
| 199 | + |
| 200 | + if (!file(extDir).directory) { |
| 201 | + throw new Exception(extDir + "NEW_RELIC_EXTENSIONS_DIR, set as '" + extDir + "'is not a valid directory.") |
| 202 | + } |
| 203 | + } |
| 204 | +} |
0 commit comments