在自己的Mojo中获取Maven插件的目录路径 [英] Get directory path of maven plugin in its own Mojo

查看:770
本文介绍了在自己的Mojo中获取Maven插件的目录路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个自定义的Maven插件.在其Mojos之一中,我正在使用以下代码段从XML文件读取Xpp3Dom对象:

I am creating a custom maven plugin. In one of its Mojos, I am reading a Xpp3Dom object from XML file using following code piece:

File pluginsFile = new File(
        "absolute-path-to-file/plugins.xml");
Xpp3Dom Xpp3DomObject = new Xpp3Dom("plugins");
try {
    FileReader reader = new FileReader(pluginsFile);
    Xpp3DomObject = Xpp3DomBuilder.build(reader);
    reader.close();
} catch (Exception e) {
    // TODO throw exception
}

我正在读取的XML文件(plugins.xml)存储在maven插件本身的src/main/resources中. 我的问题是,如何在不显式说明该文件的绝对路径的情况下指向该XML文件?

The XML file from which I am reading (plugins.xml) is stored in src/main/resources of the maven plugin itself. My question is, how do I point to that XML file without explicitly stating the absolute path to that file?

要清楚:我希望此文件位于我的maven插件的目录下.它不能在Maven插件之外,因为它是Maven插件的必要部分,并且不应由使用该插件的其他Maven项目进行更改.

To be clear: I want this file to be under the directory of my maven plugin. It cannot be outside the maven plugin as it is a necessary part of the maven plugin and should not be changed by other maven projects that consume this plugin.

我已经在Maven Mojo中搜索了一个变量/方法,该变量/方法将为我提供Maven插件本身的绝对位置.如果我知道了,那么我可以将位置指定为value-of-that-variable/src/main/resources/plugins.xml.但是我找不到这样的变量.我还尝试了一种方法,将属性从Maven插件POM传递到其Mojo之一,以便我可以传递project.build.directory,但找不到方法.

I have searched for a variable/method in Maven Mojo that would give me the absolute location of the maven plugin itself. If I get that, then I can just give the location as value-of-that-variable/src/main/resources/plugins.xml. But I am unable to find such variable. I have also tried for a way to pass properties from Maven plugin POM to one of its Mojos so that I can pass project.build.directory, but cannot find a way.

要清楚:我想访问其中一个Mojos中的maven插件目录下的文件.

To be clear: I want to access a file that is under the maven plugin directory, in one of its Mojos.

任何建议都会有所帮助.

Any suggestions will help.

推荐答案

我认为读取自己插件的一些资源文件的最简单形式是通过getResourceAsStream() API,因为您的插件迟早会以JAR,然后src目录将消失,并且将仅保留 classpath资源:

I think the easiest form to read some of the own plugin's resources files is through the getResourceAsStream() API, because sooner or later, your plugin will be delivered as a JAR, and then the src directory will dissapear and there will remain only classpath resources:

try (InputStream input=getClass().getClassLoader().getResourceAsStream("plugins.xml")){
    try(Reader reader=new InputStreamReader(input));
    {
        Xpp3Dom Xpp3DomObject = Xpp3DomBuilder.build(input);
    } catch (Exception e) {
        ...
    }
}

无论如何,以这种方式存在类路径的某些其他JAR偶然包含plugins.xml文件的风险.为了避免(或至少减少)这种风险,您应该包装它:

Anyway, in this way there is a risk that some other JAR of the classpath should contain a plugins.xml file by chance. To avoid (or at least reduce) this risk, you should package it:

src\
    main\
        resources\
            foo\
                bar\
                    MyMojo.java
                    plugins.xml

...,在这种情况下,您必须通过getClass().getResourceAsInputStream()阅读.

... and in this case, you must read it through getClass().getResourceAsInputStream().

这篇关于在自己的Mojo中获取Maven插件的目录路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆