做這些事情的最好方法是把你的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
,你可以參考你的插件。
這將從所有模塊的工作,即使父POM通過將此父的<build/>
標籤:
<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將從回購站下載並使用。
不需要相對路徑...
爲什麼不硬編碼路徑?這些東西在構建過程中不會經常變化。 –