做这些事情的最好方法是将你的 groovy 脚本放在它自己的 jar 文件中,maven-dependency-plugin
然后为你解压。然后你的脚本可以指向解压后的脚本。
像这个例子:
directory layout
+- pom.xml
+- script
| +- pom.xml
| +- src
| +- main
| +- resources
| +-computeProperties.groovy
+- code
+- pom.xml
+- src
+- main
+- java
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.stackoverflow</groupId>
<artifactId>Q11321971</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>${project.artifactId}-${project.version}</name>
<properties>
</properties>
<modules>
<module>script</module>
<module>code</module>
</modules>
<dependencyManagement>
<dependencies>
<!-- Inter-Module dependencies -->
<dependency>
<groupId>com.stackoverflow</groupId>
<artifactId>Q11321971-script</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>unpack</id>
<phase>validate</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.stackoverflow</groupId>
<artifactId>Q11321971-script</artifactId>
<overWrite>false</overWrite>
<outputDirectory>${project.build.directory}/build/groovy</outputDirectory>
<includes>**/*.groovy</includes>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
script/pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.stackoverflow</groupId>
<artifactId>Q11321971</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>Q11321971-script</artifactId>
<name>${project.artifactId}-${project.version}</name>
</project>
code/pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.stackoverflow</groupId>
<artifactId>Q11321971</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>Q11321971-code</artifactId>
<name>${project.artifactId}-${project.version}</name>
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
如果你运行这个顶级 pom,你会发现在模块的target
目录中,code
现在build/groovy/computeProperties.groovy
你可以在插件中引用它。
<build/>
通过将其添加到父级的标签中,这将适用于所有模块,即使对于父级 pom :
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
</plugin>
</plugins>
</build>
父级现在将有一个target
目录build/groovy/computeProperties.groovy
。
在您的插件中,您可以将其更改<scriptPath/>
为:
<scriptPath>${project.build.directory}/build/groovy</scriptPath>
并且由于maven-dependency-plugin
现在正在该validate
阶段运行,因此gmaven-plugin
必须在以后的阶段运行。这是您可以根据需要进行详细说明的内容。
这样做的好处是,如果有人只想签出一个子模块,那么他们可以运行它,脚本 jar 将从 repo 下载并被使用。
不需要相对路径...