【问题标题】:How to check and access javadoc/source for Maven Artifacts如何检查和访问 Maven Artifacts 的 javadoc/source
【发布时间】:2010-11-14 13:10:21
【问题描述】:

我正在编写一个 Maven 插件,它需要检查某个项目是否 依赖项有可用的 javadocs 和源代码......如果是这样,想 下载它们并将它们存档在服务器上。

我不知道如何检查 javadocs 和源代码是否可用 或者如何访问它们。

任何帮助将不胜感激。

【问题讨论】:

    标签: plugins maven-2 javadoc


    【解决方案1】:

    您可以通过将 classifier 标记添加到依赖项来引用其他工件。分类器是存储库中工件名称的附加部分,例如 junit-4.5-sources.jar

    所以要直接声明对junit源jar的依赖,你可以指定如下:

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

    如果要下载所有依赖源,请使用 maven-dependency-plugin 的 copy-dependencies 目标指定分类器源。以下示例定义了两种执行,一种用于源代码,另一种用于 javadocs。

    <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>
    

    如果要将所有下载的工件打包成 zip,可以使用 maven-assembly-plugin 创建项目的存档。下面的示例是包含源和 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>
    

    要引用程序集,请将插件配置添加到您的 pom.xml 中。这里假设上面的内容已经放在了 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>
    

    【讨论】:

      猜你喜欢
      • 2012-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多