wishihadabettername
wishihadabettername

Reputation: 14751

exec-maven-plugin not evaluating <classpath> dependencies properly

Context: I have a Maven goal that calls an executable (it happens to be a Groovy script, but it doesn't matter) which needs as an argument the path to a runtime ibrary (it happens to be a JDBC driver, but it doesn't matter either).

Instead of hardcoding the path to the driver, I'm constructing it as a classpath dependency, so that it's picked from whererever the Maven repository will happen to be (we have different environments with different OSes).

<classpath>
    <dependency>postgresql:postgresql</dependency> <!-- groupId:artifactId -->
</classpath>

Problem: the classpath is not resolved as expected, instead of the path to the library, it is empty (and therefore the invocation fails).

The relevant parts of the pom.xml are below:

<dependency>
  <groupId>postgresql</groupId>
  <artifactId>postgresql</artifactId>
  <version>9.1-901.jdbc4</version>
  <scope>provided</scope>
</dependency>


<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <version>1.2.1</version>
  <executions>
    <execution>
      <id>initialize-rule-plugin</id>
      <phase>compile</phase>
        <goals><goal>exec</goal></goals>
        <configuration>
          <executable>full\path\to\groovy</executable>
          <arguments>
            <argument>-cp</argument>
              <classpath>
                <dependency>postgresql:postgresql</dependency>
              </classpath>
            <argument>myscript.groovy</argument>
          </arguments>
        </configuration>
      </execution>
  </executions>
</plugin>

The debug output is:

[DEBUG] Configuring mojo 'org.codehaus.mojo:exec-maven-plugin:1.2.1:exec' with basic configurator -->
[DEBUG]   (s) dependency = postgresql:postgresql
[DEBUG]   (f) arguments = [-cp, Classpath { postgresql:postgresql}, ]
[...]
[DEBUG] Collected project artifacts [org.springframework:spring-context:jar:3.1.2.RELEASE:compile, org.springframework:spring-aop:jar:3.1.2.RELEASE:compile, org.springframework:spring-beans:jar:3.1.2.RELEASE:compile, org.springframework:spring-core:jar:3.1.2.RELEASE:compile, org.springframework:spring-expression:jar:3.1.2.RELEASE:compile, org.springframework:spring-asm:jar:3.1.2.RELEASE:compile, org.springframework:spring-webmvc:jar:3.1.2.RELEASE:compile, org.springframework:spring-context-support:jar:3.1.2.RELEASE:compile, org.springframework:spring-web:jar:3.1.2.RELEASE:compile, org.springframework:spring-tx:jar:3.1.2.RELEASE:compile, aopalliance:aopalliance:jar:1.0:compile, org.springframework:spring-orm:jar:3.1.2.RELEASE:compile, org.springframework:spring-jdbc:jar:3.1.2.RELEASE:compile, org.slf4j:slf4j-api:jar:1.7.1:compile, org.slf4j:jcl-over-slf4j:jar:1.7.1:runtime, org.slf4j:slf4j-log4j12:jar:1.7.1:runtime, log4j:log4j:jar:1.2.17:compile, javax.validation:validation-api:jar:1.0.0.GA:compile, org.hibernate:hibernate-validator:jar:4.3.0.Final:compile, org.jboss.logging:jboss-logging:jar:3.1.0.CR2:compile, org.hibernate:hibernate-entitymanager:jar:4.1.7.Final:compile, org.jboss.spec.javax.transaction:jboss-transaction-api_1.1_spec:jar:1.0.0.Final:compile, dom4j:dom4j:jar:1.6.1:compile, org.javassist:javassist:jar:3.15.0-GA:compile, org.hibernate:hibernate-core:jar:4.1.7.Final:compile, antlr:antlr:jar:2.7.7:compile, org.hibernate.common:hibernate-commons-annotations:jar:4.0.1.Final:compile, org.hibernate.javax.persistence:hibernate-jpa-2.0-api:jar:1.0.1.Final:compile, org.codehaus.jackson:jackson-mapper-asl:jar:1.9.10:compile, org.codehaus.jackson:jackson-core-asl:jar:1.9.10:compile, javax.servlet:jstl:jar:1.2:compile, commons-dbcp:commons-dbcp:jar:1.4:compile, commons-pool:commons-pool:jar:1.5.4:compile, org.codehaus.groovy:groovy-all:jar:2.0.5:compile]
[DEBUG] Collected project classpath [C:\projects\myproject\target\classes]
[DEBUG] Toolchains are ignored, 'executable' parameter is set to c:\full\path\to\groovy
[DEBUG] Executing command line: cmd /c c:\full\path\to\groovy.bat -cp C:\projects\myproject\target\classes myscript.groovy 

As it can be seen, it does not pick up the classpath to the driver (which exists).

I have tried with and without also including the dependency's version number in the reference.

Thank you in advance.

Upvotes: 2

Views: 1844

Answers (1)

wishihadabettername
wishihadabettername

Reputation: 14751

As per my comment, I found a different way to do the same thing. Since there was no other solution posted, I'll post it as an answer if anyone runs into the same problem.

The key is to use a different plugin, which creates, on-the-fly, properties populated with the path to a jar.

<plugin>
  <groupId>org.bitstrings.maven.plugins</groupId>
  <artifactId>dependencypath-maven-plugin</artifactId>
  <version>1.1.1</version>
  <executions>
    <execution>
      <id>set-all</id>
      <goals>
        <goal>set</goal>
      </goals>
    <configuration>
      <propertySets>
        <propertySet>
          <includes>
            <include>postgresql:postgresql:jar</include>
          </includes>
        </propertySet>
      </propertySets>
    </configuration>
    </execution>
  </executions>
</plugin>

[...]

<execution>
  <id>my-script</id>
  <phase>compile</phase>
  <goals>
    <goal>exec</goal>
  </goals>
  <configuration>
    <executable>path/to/groovy</executable>
    <arguments>
      <argument>-cp</argument>
      <argument>${postgresql:postgresql:jar}</argument>
      <argument>my-script.groovy</argument>
    </arguments>
  </configuration>
</execution>

Upvotes: 1

Related Questions