1 package org.jetbrains.dokka.maven 2 3 import org.apache.maven.archiver.MavenArchiveConfiguration 4 import org.apache.maven.archiver.MavenArchiver 5 import org.apache.maven.execution.MavenSession 6 import org.apache.maven.plugin.AbstractMojo 7 import org.apache.maven.plugins.annotations.* 8 import org.apache.maven.project.MavenProject 9 import org.apache.maven.project.MavenProjectHelper 10 import org.codehaus.plexus.archiver.Archiver 11 import org.codehaus.plexus.archiver.jar.JarArchiver 12 import org.jetbrains.dokka.* 13 import java.io.File 14 import java.net.URL 15 16 class SourceLinkMapItem { 17 @Parameter(name = "dir", required = true) 18 var dir: String = "" 19 20 @Parameter(name = "url", required = true) 21 var url: String = "" 22 23 @Parameter(name = "urlSuffix") 24 var urlSuffix: String? = null 25 } 26 27 class ExternalDocumentationLinkBuilder : DokkaConfiguration.ExternalDocumentationLink.Builder() { 28 29 @Parameter(name = "url", required = true) 30 override var url: URL? = null 31 @Parameter(name = "packageListUrl", required = true) 32 override var packageListUrl: URL? = null 33 } 34 35 abstract class AbstractDokkaMojo : AbstractMojo() { 36 class SourceRoot : DokkaConfiguration.SourceRoot { 37 @Parameter(required = true) 38 override var path: String = "" 39 40 @Parameter 41 override var platforms: List<String> = emptyList() 42 } 43 44 class PackageOptions : DokkaConfiguration.PackageOptions { 45 @Parameter 46 override var prefix: String = "" 47 @Parameter 48 override var includeNonPublic: Boolean = false 49 @Parameter 50 override var reportUndocumented: Boolean = true 51 @Parameter 52 override var skipDeprecated: Boolean = false 53 @Parameter 54 override var suppress: Boolean = false 55 } 56 57 @Parameter(required = true, defaultValue = "\${project.compileSourceRoots}") 58 var sourceDirectories: List<String> = emptyList() 59 60 @Parameter 61 var sourceRoots: List<SourceRoot> = emptyList() 62 63 @Parameter 64 var samplesDirs: List<String> = emptyList() 65 66 @Parameter 67 @Deprecated("Use <includes> instead") 68 var includeDirs: List<String> = emptyList() 69 70 @Parameter 71 var includes: List<String> = emptyList() 72 73 @Parameter(required = true, defaultValue = "\${project.compileClasspathElements}") 74 var classpath: List<String> = emptyList() 75 76 @Parameter 77 var sourceLinks: Array<SourceLinkMapItem> = emptyArray() 78 79 @Parameter(required = true, defaultValue = "\${project.artifactId}") 80 var moduleName: String = "" 81 82 @Parameter(required = false, defaultValue = "false") 83 var skip: Boolean = false 84 85 @Parameter(required = false, defaultValue = "6") 86 var jdkVersion: Int = 6 87 88 @Parameter 89 var skipDeprecated = false 90 @Parameter 91 var skipEmptyPackages = true 92 @Parameter 93 var reportNotDocumented = true 94 95 @Parameter 96 var impliedPlatforms: List<String> = emptyList() 97 98 @Parameter 99 var perPackageOptions: List<PackageOptions> = emptyList() 100 101 @Parameter 102 var externalDocumentationLinks: List<ExternalDocumentationLinkBuilder> = emptyList() 103 104 @Parameter(defaultValue = "false") 105 var noStdlibLink: Boolean = false 106 107 @Parameter(defaultValue = "false") 108 var noJdkLink: Boolean = false 109 110 @Parameter 111 var cacheRoot: String? = null 112 113 @Parameter 114 var languageVersion: String? = null 115 116 @Parameter 117 var apiVersion: String? = null 118 getOutDirnull119 protected abstract fun getOutDir(): String 120 protected abstract fun getOutFormat(): String 121 122 override fun execute() { 123 if (skip) { 124 log.info("Dokka skip parameter is true so no dokka output will be produced") 125 return 126 } 127 128 val gen = DokkaGenerator( 129 MavenDokkaLogger(log), 130 classpath, 131 sourceDirectories.map { SourceRootImpl(it) } + sourceRoots, 132 samplesDirs, 133 includeDirs + includes, 134 moduleName, 135 DocumentationOptions(getOutDir(), getOutFormat(), 136 sourceLinks = sourceLinks.map { SourceLinkDefinitionImpl(it.dir, it.url, it.urlSuffix) }, 137 jdkVersion = jdkVersion, 138 skipDeprecated = skipDeprecated, 139 skipEmptyPackages = skipEmptyPackages, 140 reportUndocumented = reportNotDocumented, 141 impliedPlatforms = impliedPlatforms, 142 perPackageOptions = perPackageOptions, 143 externalDocumentationLinks = externalDocumentationLinks.map { it.build() }, 144 noStdlibLink = noStdlibLink, 145 noJdkLink = noJdkLink, 146 cacheRoot = cacheRoot, 147 languageVersion = languageVersion, 148 apiVersion = apiVersion 149 ) 150 ) 151 152 gen.generate() 153 } 154 } 155 156 @Mojo(name = "dokka", defaultPhase = LifecyclePhase.PRE_SITE, threadSafe = true, requiresDependencyResolution = ResolutionScope.COMPILE, requiresProject = true) 157 class DokkaMojo : AbstractDokkaMojo() { 158 @Parameter(required = true, defaultValue = "html") 159 var outputFormat: String = "html" 160 161 @Parameter(required = true, defaultValue = "\${project.basedir}/target/dokka") 162 var outputDir: String = "" 163 getOutFormatnull164 override fun getOutFormat() = outputFormat 165 override fun getOutDir() = outputDir 166 } 167 168 @Mojo(name = "javadoc", defaultPhase = LifecyclePhase.PRE_SITE, threadSafe = true, requiresDependencyResolution = ResolutionScope.COMPILE, requiresProject = true) 169 class DokkaJavadocMojo : AbstractDokkaMojo() { 170 @Parameter(required = true, defaultValue = "\${project.basedir}/target/dokkaJavadoc") 171 var outputDir: String = "" 172 173 override fun getOutFormat() = "javadoc" 174 override fun getOutDir() = outputDir 175 } 176 177 @Mojo(name = "javadocJar", defaultPhase = LifecyclePhase.PRE_SITE, threadSafe = true, requiresDependencyResolution = ResolutionScope.COMPILE, requiresProject = true) 178 class DokkaJavadocJarMojo : AbstractDokkaMojo() { 179 @Parameter(required = true, defaultValue = "\${project.basedir}/target/dokkaJavadocJar") 180 var outputDir: String = "" 181 182 /** 183 * Specifies the directory where the generated jar file will be put. 184 */ 185 @Parameter(property = "project.build.directory") 186 private var jarOutputDirectory: String? = null 187 188 /** 189 * Specifies the filename that will be used for the generated jar file. Please note that `-javadoc` 190 * or `-test-javadoc` will be appended to the file name. 191 */ 192 @Parameter(property = "project.build.finalName") 193 private var finalName: String? = null 194 195 /** 196 * Specifies whether to attach the generated artifact to the project helper. 197 */ 198 @Parameter(property = "attach", defaultValue = "true") 199 private val attach: Boolean = false 200 201 /** 202 * The archive configuration to use. 203 * See [Maven Archiver Reference](http://maven.apache.org/shared/maven-archiver/index.html) 204 */ 205 @Parameter 206 private val archive = MavenArchiveConfiguration() 207 208 @Parameter(property = "maven.javadoc.classifier", defaultValue = "javadoc", required = true) 209 private var classifier: String? = null 210 211 @Parameter(defaultValue = "\${session}", readonly = true, required = true) 212 protected var session: MavenSession? = null 213 214 @Parameter(defaultValue = "\${project}", readonly = true, required = true) 215 protected var project: MavenProject? = null 216 217 @Component 218 private var projectHelper: MavenProjectHelper? = null 219 220 @Component(role = Archiver::class, hint = "jar") 221 private var jarArchiver: JarArchiver? = null 222 getOutFormatnull223 override fun getOutFormat() = "javadoc" 224 override fun getOutDir() = outputDir 225 226 override fun execute() { 227 super.execute() 228 if(!File(outputDir).exists()) { 229 log.warn("No javadoc generated so no javadoc jar will be generated") 230 return 231 } 232 val outputFile = generateArchive("$finalName-$classifier.jar") 233 if (attach) { 234 projectHelper?.attachArtifact(project, "javadoc", classifier, outputFile) 235 } 236 } 237 generateArchivenull238 private fun generateArchive(jarFileName: String): File { 239 val javadocJar = File(jarOutputDirectory, jarFileName) 240 241 val archiver = MavenArchiver() 242 archiver.setArchiver(jarArchiver) 243 archiver.setOutputFile(javadocJar) 244 archiver.archiver.addDirectory(File(outputDir), arrayOf("**/**"), arrayOf()) 245 246 archive.setAddMavenDescriptor(false) 247 archiver.createArchive(session, project, archive) 248 249 return javadocJar 250 } 251 } 252 253