tags:

views:

36

answers:

3

Hi All;

Is there anyway to get the maven-jar-plugin to use scope when adding a classpath to a jar manifest? I have a project where I want to create 2 jars - runtime and test. The runtime jar should have a classpath of only the runtime dependencies. The test jar should have a classpath of the test dependencies. I have not been able to figure out how to do this. any ideas?

I am aware of MJAR-117, but this bug is over a year old - perhaps it has been resolved in a different JIRA?

A: 

I am not aware if the bug is fixed. To be frank, I wasn't even aware if this bug existed. But I think your problem can be solved using maven profiles

You can have a separate profile for your testing as :

<profile>
  <id>test</id>
  <activation>
    <property>
      <name>test</name>
      <value>true</value>
    </property>
  </activation>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8</version>
      <!-- 
        we don't need the test scope since we want the jar plugin to include
        it in classpath 
      -->
      <!-- scope>test</scope --> 
    </dependency>
  </dependencies>
</profile>

To use this profile:

$>mvn -Dtest=true package
naikus
A: 

I would like to create the app jar and the test jar in one execution of mvn. By using profiles, I would have to execute mvn twice. The reason why I need one execution of mvn is because I want to use the maven-assembly-plugin to package these 2 jars together in a zip file.

I went into the sourcecode for maven archiver (here). Looks like this is not possible. I think the only way I can do this is by marking the scopes of all my test dependencies as "runtime" and just deploying these jars to PROD even when they will not be used. Not pretty.

196 if ( config.isAddClasspath() ) 197 { 198 StringBuffer classpath = new StringBuffer(); 199
200 List artifacts = project.getRuntimeClasspathElements();

i just had another thought - could this be accomplished with the maven groovy plugin?

Paul Rowe
+1  A: 

I don't think this is supported by the Maven Archiver (and MJAR-117 doesn't seem to get much traction). A possible workaround would be to provide (hard-coded) additional classpath entries when building the test-jar:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <version>2.2</version>
  <executions>
    <execution>
      <id>default-jar</id>
      <configuration>
        <archive>
          <manifest>
            <addClasspath>true</addClasspath>
          </manifest>
        </archive>
      </configuration>
    </execution>
    <execution>
      <id>default-test-jar</id>
      <phase>package</phase>
      <goals>
        <goal>test-jar</goal>
      </goals>
      <configuration>
        <archive>
          <manifest>
            <addClasspath>true</addClasspath>
          </manifest>
          <manifestEntries>
            <Class-Path>foo-1.0.jar bar-2.1.jar</Class-Path>
          </manifestEntries>
        </archive>
      </configuration>
    </execution>
  </executions>
</plugin>

I agree this is not ideal, you have to add things manually and this is error-prone. But it works.

You could maybe do something more dynamic with filtering and some antrun or groovy magic but this would definitely require more work.

Related question

Pascal Thivent