[英]How to let plugin execution inherit project dependencies with Maven
I'm using Maven and I would like to execute a plugin without repeating some of the required dependencies: 我正在使用Maven,并且想执行插件而不重复某些必需的依赖项:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sql-maven-plugin</artifactId>
<version>1.5</version>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.3.168</version>
</dependency>
<!-- ^^^ unnecessary duplication, IMO, because the project
already imports the dependency below -->
</dependencies>
<!-- ... -->
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.3.168</version>
</dependency>
</dependencies>
In the above example, I would like to omit the com.h2database:h2
dependency, because I have already specified that in the project. 在上面的示例中,我想省略
com.h2database:h2
依赖项,因为我已经在项目中指定了它。 Can this be done? 能做到吗? How?
怎么样?
You can do that by using the pluginManagement block in your parent like this: 您可以这样使用父级中的pluginManagement块来做到这一点:
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sql-maven-plugin</artifactId>
<version>1.5</version>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.3.168</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</pluginManagement>
Within your childs you only need to use the execution like this: 在您的孩子中,您只需要使用以下执行方式:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sql-maven-plugin</artifactId>
<executions>
<execution>
....
</execution>
</executions>
</plugin>
</plugins>
</build>
This will solve your problem to maintain the supplemental classpath dependencies (h2) only a single place. 这将解决您的问题,以仅将补充类路径依赖项(h2)保持在单个位置。
While plugins don't automatically inherit dependencies from the modules / projects that include them ( see khmarbaise's answer ), it is still possible for a plugin author to implement their plugin in a way for the module / project classpath to be available to the plugin as well. 尽管插件不会自动从包含它们的模块/项目中继承依赖项( 请参见khmarbaise的答案 ),但是插件作者仍然有可能以模块/项目类路径可用于该插件的方式实现其插件。好。 The Flyway migration plugin or the jOOQ code generator do that, for instance.
例如, Flyway 迁移插件或jOOQ代码生成器就可以做到这一点。
One example how this can be done inside the plugin is this: 如何在插件内部完成此操作的一个示例是:
@Mojo
public class Plugin extends AbstractMojo {
@Parameter(property = "project", required = true, readonly = true)
private MavenProject project;
@Override
public void execute() throws MojoExecutionException {
ClassLoader oldCL = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(getClassLoader());
// Plugin logic
}
finally {
Thread.currentThread().setContextClassLoader(oldCL);
}
}
@SuppressWarnings("unchecked")
private ClassLoader getClassLoader() throws MojoExecutionException {
try {
List<String> classpathElements = project.getRuntimeClasspathElements();
URL urls[] = new URL[classpathElements.size()];
for (int i = 0; i < urls.length; i++)
urls[i] = new File(classpathElements.get(i)).toURI().toURL();
return new URLClassLoader(urls, getClass().getClassLoader());
}
catch (Exception e) {
throw new MojoExecutionException("Couldn't create a classloader.", e);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.