简体   繁体   English

如何检查和访问Maven工件的javadoc / source

[英]How to check and access javadoc/source for Maven Artifacts

I am writing a Maven plugin which needs to check if a certain project dependency has javadocs and sources available... and if so, would like to download them and archive them on a server. 我正在编写一个Maven插件,需要检查某个项目依赖项是否有javadoc和源可用...如果是,想要下载它们并将它们存档在服务器上。

I cannot find out how to check if the javadocs and source are available or how to access them if they are. 我无法找到如何检查javadoc和源是否可用或如何访问它们。

Any help would be appreciated. 任何帮助,将不胜感激。

You can reference additional artifacts by adding the classifier tag to a dependency. 您可以通过将分类器标记添加到依赖项来引用其他工件。 The classifier is the additional part of the artifact's name in the repository, eg junit-4.5- sources .jar 分类器是存储库中工件名称的附加部分,例如junit-4.5- sources .jar

So to directly declare a dependency on the junit sources jar you can specify it as follows: 因此,要直接声明对junit源jar的依赖,可以按如下方式指定它:

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.5</version>
  <classifier>sources</classifier>
  <scope>test</scope>
</dependency>

If you want to download all the dependency sources, use the maven-dependency-plugin's copy-dependencies goal specifying the classifier sources. 如果要下载所有依赖项源,请使用maven-dependency-plugin的copy-dependencies目标指定分类器源。 The following example defines two executions, one for sources and one for javadocs. 以下示例定义了两个执行,一个用于源,一个用于javadoc。

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-dependency-plugin</artifactId>
      <executions>
        <execution>
          <id>sources</id>
          <phase>package</phase>
          <goals>
            <goal>copy-dependencies</goal>
          </goals>
          <configuration>
            <classifier>sources</classifier>
            <failOnMissingClassifierArtifact>false</failOnMissingClassifierArtifact>
            <outputDirectory>${project.build.directory}/sources</outputDirectory>
          </configuration>
        </execution>
        <execution>
          <id>javadocs</id>
          <phase>package</phase>
          <goals>
            <goal>copy-dependencies</goal>
          </goals>
          <configuration>
            <classifier>javadoc</classifier>
            <failOnMissingClassifierArtifact>false</failOnMissingClassifierArtifact>
            <outputDirectory>${project.build.directory}/javadocs</outputDirectory>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

If you want to package all the downloaded artifacts into a zip, you can use the maven-assembly-plugin to create an archive of the project. 如果要将所有下载的工件打包成zip,可以使用maven-assembly-plugin创建项目的存档。 The example below are the contents of an assembly descriptor file to include the sources and javadocs directories: 下面的示例是包含sources和javadocs目录的程序集描述符文件的内容:

<assembly>
  <id>project</id>
  <formats>
    <format>zip</format>
  </formats>
  <fileSets>
    <fileSet>
      <directory>${project.basedir}</directory>
      <useDefaultExcludes>true</useDefaultExcludes>
      <includes>
        <include>${project.build.directory}/sources</include>
        <include>${project.build.directory}/javadocs</include>
      </includes>
    </fileSet>
  </fileSets>
</assembly>

To reference the assembly, add a plugin configuration to your pom. 要引用程序集,请将插件配置添加到pom中。 This assumes the above contents have been put in src/main/assembly/sources.xml (make sure it is defined after the dependency configuration above): 这假定上面的内容已经放在src / main / assembly / sources.xml中(确保它是在上面的依赖配置之后定义的):

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <version>2.2-beta-4</version>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
      <configuration>
        <descriptors>
           <descriptor>src/main/assembly/sources.xml</descriptor>
        </descriptors>
      </configuration>
    </execution>
  </executions>
</plugin>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM