Maven compile a source jar dependency

前端 未结 2 1068
粉色の甜心
粉色の甜心 2021-02-01 23:18

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

相关标签:
2条回答
  • 2021-02-02 00:06

    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>
    
    0 讨论(0)
  • 2021-02-02 00:15

    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>
    
    0 讨论(0)
提交回复
热议问题