Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven compile a source jar dependency

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.

like image 633
MShekow Avatar asked Sep 15 '11 14:09

MShekow


People also ask

How do I create a source jar?

The source plugin can be used to create a jar file of the project sources from the command line or by binding the goal to the project's build lifecycle. To generate the jar from the command line, use the following command: mvn source:jar.

Does Maven compile download dependencies?

Maven automatically downloads your project's dependencies, whenever you run a build.

Which command should you use to package source code into jar file in Maven?

Execute the “mvn clean install” command to built the project and deploy the jar files to the target folder.


1 Answers

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>
like image 166
Raghuram Avatar answered Sep 18 '22 18:09

Raghuram