Maven编译一个源jar依赖 [英] Maven compile a source jar dependency

查看:155
本文介绍了Maven编译一个源jar依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个项目使用一个可以在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.

推荐答案

您可以执行以下操作:


  1. 使用maven依赖插件
  2. 使用目录并将依赖项的内容放置到文件夹中
  3. build-helper-maven-plugin /> build-helper-maven-plugin add-source 目标将此文件夹添加为源文件夹
  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>

这篇关于Maven编译一个源jar依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆