上下文:我有一个调用可执行文件的Maven目标(它恰好是一个Groovy脚本,但无所谓),它需要一个参数作为运行时ibrary的路径(它碰巧是JDBC驱动程序,但它也无关紧要。)
我没有硬编码驱动程序的路径,而是将其构建为classpath dependency
,以便从Maven存储库所在的位置选择它(我们有不同的操作系统的不同环境)。
<classpath>
<dependency>postgresql:postgresql</dependency> <!-- groupId:artifactId -->
</classpath>
问题:类路径未按预期解析,而不是库的路径,它是空的(因此调用失败)。
pom.xml的相关部分如下:
<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>
调试输出为:
[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
可以看出,它没有获取驱动程序的类路径(存在)。
我已尝试使用和不使用参考中的依赖项版本号。
提前谢谢。
答案 0 :(得分:1)
根据我的评论,我找到了另一种方法来做同样的事情。由于没有其他解决方案发布,如果有人遇到同样的问题,我会将其发布为答案。
关键是使用另一个插件,它可以动态创建填充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>