Maven编译源jar依赖

发布于 2024-12-04 12:23:33 字数 272 浏览 2 评论 0原文

假设我有一个项目使用可以在 Maven 存储库中找到的依赖项。然而,我们还要说的是,将要下载的 jar 文件的格式不适合我的项目(例如,如果我的 maven 项目是 Android 项目,并且 jar 的编译方式是 dex 工具不喜欢的) 。我想做的是下载该依赖项的源版本。然而,一旦我添加了 java-source,这些类就无法再从我自己的源代码中访问了。我希望maven下载源jar并编译其中的java文件,并将编译后的类文件放置在类路径中。这可能吗?

我唯一的选择是自己创建一个包含该库的新项目,但这很麻烦。

Let's say I have a project that uses a dependency that can be found in the Maven repository. However, lets also say that the jar file that will be downloaded is NOT in a format that is suitable for my project (e.g. if my maven project is an Android project and the jar has been compiled in a way the dex tool does not like). What I want to do instead is to downloaded the source version of that dependency. Once I add java-source, however, the classes are not accessible anymore from my own source code. I would like that maven downloads the source jar and compiles the java files inside it and places the compiled class files in the classpath. Is that possible?

My only alternative is to create a new project containing that library myself, but that's cumbersome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

牵你手 2024-12-11 12:23:33

您可以执行以下操作:

  1. 使用 maven 依赖插件解压 目标并将内容放置在依赖到文件夹
  2. 使用 build-helper-maven-plugin 的 < a href="https://www.mojohaus.org/build-helper-maven-plugin/usage.html" rel="nofollow noreferrer">add-source 目标将此文件夹添加为源文件夹

此处是一些代码片段...

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-dependency-plugin</artifactId>
  <version>2.2</version>
  <executions>
    <execution>
      <id>unpack</id>
      <phase>process-sources</phase>
      <goals>
        <goal>unpack</goal>
      </goals>
      <configuration>
        <artifactItems>
          <artifactItem>
            <groupId>my.group</groupId>
            <artifactId>my.artifact</artifactId>
            <version>my.artifact.version</version>
            <classifier>sources</classifier>
            <overWrite>false</overWrite>
            <outputDirectory>${project.build.directory}/my.artifact</outputDirectory>
          </artifactItem>
        </artifactItems>
      </configuration>
    </execution>
  </executions>
</plugin>
<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>build-helper-maven-plugin</artifactId>
  <version>1.5</version>
  <executions>
    <execution>
      <id>add-source</id>
      <phase>generate-sources</phase>
      <goals>
        <goal>add-source</goal>
      </goals>
      <configuration>
        <sources>
          <source>${project.build.directory}/my.artifact.source</source>
        </sources>
      </configuration>
    </execution>
  </executions>
</plugin>

You could do the following:

  1. Use maven dependency plugin's unpack goal and place the contents of the dependency into a folder
  2. Use build-helper-maven-plugin's add-source goal to add this folder as a source folder

Here is some code snippet...

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-dependency-plugin</artifactId>
  <version>2.2</version>
  <executions>
    <execution>
      <id>unpack</id>
      <phase>process-sources</phase>
      <goals>
        <goal>unpack</goal>
      </goals>
      <configuration>
        <artifactItems>
          <artifactItem>
            <groupId>my.group</groupId>
            <artifactId>my.artifact</artifactId>
            <version>my.artifact.version</version>
            <classifier>sources</classifier>
            <overWrite>false</overWrite>
            <outputDirectory>${project.build.directory}/my.artifact</outputDirectory>
          </artifactItem>
        </artifactItems>
      </configuration>
    </execution>
  </executions>
</plugin>
<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>build-helper-maven-plugin</artifactId>
  <version>1.5</version>
  <executions>
    <execution>
      <id>add-source</id>
      <phase>generate-sources</phase>
      <goals>
        <goal>add-source</goal>
      </goals>
      <configuration>
        <sources>
          <source>${project.build.directory}/my.artifact.source</source>
        </sources>
      </configuration>
    </execution>
  </executions>
</plugin>
用心笑 2024-12-11 12:23:33

使用 Maven 下载源包很简单:

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.16</version>
    <classifier>sources</classifier>
</dependency>

如何配置 Maven 来扩展此依赖项,然后编译其内容超出了我的范围......

您是否考虑过 ANT 解决方案? ivy 插件 为其提供了类似 Maven 的功能,并且 groovy 插件 可用于编写您的特殊构建逻辑的脚本:

build.xml

Ivy 使用“配置”(类似于梅文范围)来对依赖项进行分组。

在此示例中,“sources”配置保存下载的源包。它们被放置到一个引用的文件集中,该文件集可以由 groovy 任务按顺序处理。

每个下载的源 jar 都会解压缩到“build/src”目录中:

<project name="demo" default="unzip-sources" xmlns:ivy="antlib:org.apache.ivy.ant">

    <property name="build.dir" location="build"/>
    <property name="src.dir"   location="${build.dir}/src"/>

    <target name="resolve">
        <ivy:resolve/>
        <ivy:cachepath pathid="build.path" conf="build"/>
        <ivy:cachefileset setid="sourcezips" conf="sources"/>
    </target>

    <target name="unzip-sources" depends="resolve">
        <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="build.path"/>

        <groovy>
        project.references.sourcezips.each {
            ant.unzip(src: it, dest: properties["src.dir"])
        }
        </groovy>
    </target>

    <target name="clean">
        <delete dir="${build.dir}"/>
    </target>

</project>

ivy.xml

每个源包依赖项都使用“source”配置。这直接映射到 Maven 模块的“源”范围。

<ivy-module version="2.0">
    <info organisation="org.myspotontheweb" module="demo"/>
    <configurations>
        <conf name="build" description="ANT tasks"/>
        <conf name="sources" description="Source packages"/>
    </configurations>
    <dependencies>
        <!-- Build dependencies -->
        <dependency org="org.codehaus.groovy" name="groovy-all" rev="1.8.2" conf="build->default"/>

        <!-- Source dependencies -->
        <dependency org="log4j" name="log4j" rev="1.2.16" conf="sources"/>
        <dependency org="commons-lang" name="commons-lang" rev="2.6" conf="sources"/>
    </dependencies>
</ivy-module>

Downloading the source packages using Maven is easy:

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.16</version>
    <classifier>sources</classifier>
</dependency>

How to configure Maven to expand this dependency and then compile it's contents is beyond me....

Have you considered an ANT solution? The ivy plug-in provides it with Maven-like abilities and the groovy plug-in can be used to script your special build logic:

build.xml

Ivy uses "configurations" (similar to Maven scopes) to group dependencies.

In this example the "sources" configuration holds the downloaded source packages. These are placed into a referenced fileset, which can be processed sequentially by the groovy task.

Each downloaded source jar is unzipped into the "build/src" directory:

<project name="demo" default="unzip-sources" xmlns:ivy="antlib:org.apache.ivy.ant">

    <property name="build.dir" location="build"/>
    <property name="src.dir"   location="${build.dir}/src"/>

    <target name="resolve">
        <ivy:resolve/>
        <ivy:cachepath pathid="build.path" conf="build"/>
        <ivy:cachefileset setid="sourcezips" conf="sources"/>
    </target>

    <target name="unzip-sources" depends="resolve">
        <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="build.path"/>

        <groovy>
        project.references.sourcezips.each {
            ant.unzip(src: it, dest: properties["src.dir"])
        }
        </groovy>
    </target>

    <target name="clean">
        <delete dir="${build.dir}"/>
    </target>

</project>

ivy.xml

Each source package dependency uses the "sources" configuration. This maps directly to the "sources" scope of the Maven module.

<ivy-module version="2.0">
    <info organisation="org.myspotontheweb" module="demo"/>
    <configurations>
        <conf name="build" description="ANT tasks"/>
        <conf name="sources" description="Source packages"/>
    </configurations>
    <dependencies>
        <!-- Build dependencies -->
        <dependency org="org.codehaus.groovy" name="groovy-all" rev="1.8.2" conf="build->default"/>

        <!-- Source dependencies -->
        <dependency org="log4j" name="log4j" rev="1.2.16" conf="sources"/>
        <dependency org="commons-lang" name="commons-lang" rev="2.6" conf="sources"/>
    </dependencies>
</ivy-module>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文