ExportLicensesTask.groovy revision f5517fe4100cc751b365edb43a07455b7b435215
1/* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16package android.databinding 17 18import org.gradle.api.DefaultTask; 19import org.gradle.api.artifacts.ResolvedArtifact 20import org.gradle.api.tasks.TaskAction; 21 22class ExportLicensesTask extends DefaultTask { 23 List<ResolvedArtifact> artifacts = new ArrayList(); 24 25 static def knownLicenses = [ 26 [ 27 libraries: ["kotlin-stdlib", "kotlin-runtime", "kotlin-annotation-processing", "kotlin-gradle-plugin", "kotlin-gradle-plugin-api"], 28 licenses : ["https://raw.githubusercontent.com/JetBrains/kotlin/master/license/LICENSE.txt", 29 "http://www.apache.org/licenses/LICENSE-2.0.txt"], 30 notices : ["https://raw.githubusercontent.com/JetBrains/kotlin/master/license/NOTICE.txt"] 31 ], 32 [ 33 libraries: ["antlr4", "antlr4-runtime", "antlr-runtime", "antlr4-annotations"], 34 licenses : ["https://raw.githubusercontent.com/antlr/antlr4/master/LICENSE.txt"] 35 ], 36 [ 37 libraries: ["antlr"], 38 licenses: ["http://www.antlr3.org/license.html", "http://www.antlr2.org/license.html"] 39 ], 40 [ 41 libraries: ["java.g4"], 42 licenses : ["https://raw.githubusercontent.com/antlr/antlr4/master/LICENSE.txt"] 43 ], 44 [ 45 libraries: ["ST4", "stringtemplate"], 46 licenses : ["https://raw.githubusercontent.com/antlr/stringtemplate4/master/LICENSE.txt"] 47 ], 48 [ 49 libraries: ["org.abego.treelayout.core"], 50 licenses : ["http://treelayout.googlecode.com/files/LICENSE.TXT"] 51 ], 52 [ 53 libraries: ["junit"], 54 licenses : ["https://raw.githubusercontent.com/junit-team/junit/master/LICENSE-junit.txt"], 55 notices : ["https://raw.githubusercontent.com/junit-team/junit/master/NOTICE.txt"] 56 ], 57 [ 58 libraries: ["commons-io"], 59 licenses : ["http://svn.apache.org/viewvc/commons/proper/io/trunk/LICENSE.txt?view=co"], 60 notices : ["http://svn.apache.org/viewvc/commons/proper/io/trunk/NOTICE.txt?view=co"] 61 ], 62 [ 63 libraries: ["commons-codec"], 64 licenses: ["http://svn.apache.org/viewvc/commons/proper/codec/trunk/LICENSE.txt?view=co"], 65 notices: ["http://svn.apache.org/viewvc/commons/proper/codec/trunk/NOTICE.txt?view=co"] 66 ], 67 [ 68 libraries: ["commons-lang3"], 69 licenses : ["https://git-wip-us.apache.org/repos/asf?p=commons-lang.git;a=blob_plain;f=LICENSE.txt;hb=refs/heads/master"], 70 notices : ["https://git-wip-us.apache.org/repos/asf?p=commons-lang.git;a=blob_plain;f=NOTICE.txt;hb=refs/heads/master"] 71 ], 72 [ 73 libraries: ["guava"], 74 licenses : ["http://www.apache.org/licenses/LICENSE-2.0.txt"] 75 ], 76 [ 77 libraries: ["hamcrest-core"], 78 licenses : ["https://raw.githubusercontent.com/hamcrest/JavaHamcrest/master/LICENSE.txt"] 79 ], 80 [ 81 libraries: ["avalon-framework"], 82 licenses : ["http://archive.apache.org/dist/avalon/LICENSE.txt"] 83 ], 84 [ 85 libraries: ["log4j"], 86 licenses: ["https://git-wip-us.apache.org/repos/asf?p=logging-log4j2.git;a=blob_plain;f=LICENSE.txt;hb=HEAD"], 87 notices: ["https://git-wip-us.apache.org/repos/asf?p=logging-log4j2.git;a=blob_plain;f=NOTICE.txt;hb=HEAD"] 88 ], 89 [ 90 libraries: ["ant", "ant-launcher"], 91 licenses: ["http://www.apache.org/licenses/LICENSE-2.0.html"] 92 ], 93 [ 94 libraries: ["xz"], 95 licenses: ["http://git.tukaani.org/?p=xz-java.git;a=blob_plain;f=COPYING;hb=HEAD"] 96 ], 97 [ 98 libraries: ["logkit"], 99 licenses: ["unknown. see: http://commons.apache.org/proper/commons-logging/dependencies.html"] 100 ] 101 102 ] 103 104 Map<String, Object> usedLicenses = new HashMap<>(); 105 static Map<String, Object> licenseLookup = new HashMap<>(); 106 static { 107 knownLicenses.each {license -> 108 license.libraries.each { 109 licenseLookup.put(it, license) 110 } 111 } 112 } 113 114 ExportLicensesTask() { 115 } 116 117 public void add(ResolvedArtifact artifact) { 118 artifacts.add(artifact) 119 println("adding artifact $artifact") 120 } 121 122 @TaskAction 123 public void exportNotice() { 124 project.configurations.compile.getResolvedConfiguration().getResolvedArtifacts().each { 125 add(it) 126 } 127 resolveLicenses() 128 def notice = buildNotice(usedLicenses) 129 def noticeFile = new File(project.buildDir,'NOTICE.txt') 130 noticeFile.delete() 131 println ("writing notice file to: ${noticeFile.getAbsolutePath()}") 132 noticeFile << notice 133 } 134 135 public void resolveLicenses() { 136 artifacts.each { artifact -> 137 if (!shouldSkip(artifact)) { 138 def license = licenseLookup.get(artifact.name) 139 if (license == null) { 140 throw new RuntimeException("Cannot find license for ${artifact.getModuleVersion().id} in ${artifact.getFile()}") 141 } 142 usedLicenses.put(artifact, license) 143 } 144 } 145 } 146 147 public static Object findLicenseFor(String artifactId) { 148 return licenseLookup.get(artifactId) 149 } 150 151 public static String urlToText(String url) { 152 //return new URL(url).getText() 153 return url 154 } 155 156 public boolean shouldSkip(ResolvedArtifact artifact) { 157 return artifact.getModuleVersion().id.group.startsWith("com.android"); 158 } 159 160 public static String buildNotice(Map<String, Object> licenses) { 161 // now build the output 162 StringBuilder notice = new StringBuilder(); 163 notice.append("List of 3rd party licenses:") 164 licenses.each { 165 notice.append("\n-----------------------------------------------------------------------------") 166 notice.append("\n* ${it.key}") 167 notice.append("\n") 168 def license = it.value 169 if (license.notices != null) { 170 license.notices.each { 171 notice.append("\n ****** NOTICE:\n${urlToText(it)}") 172 } 173 } 174 license.licenses.each { 175 notice.append("\n ****** LICENSE:\n${urlToText(it)}") 176 } 177 notice.append("\n\n\n") 178 } 179 return notice.toString() 180 } 181}