我编写了一个 Maven 插件,在独立运行时工作正常。我在插件源代码中有一些自定义 XML 资源文件,这些文件在测试时对插件可见和可访问(下面的插件 POM)。所以我安装了插件,一切都很好。
现在我创建一个新项目,它引用新安装的插件(下面的项目 POM)。我有一些类似于嵌入在插件项目中的自定义 XML 资源文件。
该项目具有以下结构:
tester
| pom.xml
|
+---src
| +---main
| | +---java
| | | \---[BLAH}
| | |
| | \---resources
| | tester-catalog-env.xml
| |
| \---test
| \---java
| \---[BLAH]
在插件中,我有以下方法:
public TesterProcessCatalog getTesterProcessCatalog(Properties properties) throws TesterDataSourceException {
try {
InputStream in = getClass().getClass().getResourceAsStream("tester-catalog-env.xml");
Reader reader = ReaderFactory.newXmlReader(in);
return readCatalog(reader);
} catch (IOException e) {
throw new TesterDataSourceException("Error reading catalog", e);
}
}
该行:
InputStream in = getClass().getClass().getResourceAsStream("tester-catalog-env.xml");
使用(如插件 mojo 中定义的)从项目运行时返回 null,mvn tester:test
即使我可以看到资源存在于类路径中。我试过以下
InputStream in = getClass().getClass().getResourceAsStream("/tester-catalog-env.xml");
这也返回null
。
如果我将资源文件复制到插件资源目录并运行,它工作正常。
最终我想要实现的是在项目资源目录中拥有多个配置文件(即 tester-catalog-xxx.xml - 其中 xxx 可以是用户创建的新文件),当mvn tester:test -DprocessCatalog=env2
运行类似命令时文件名为 tester-catalog-env2.xml 将被加载。
关于我做错了什么的任何想法?
Maven信息
Apache Maven 3.0.4 (r1232337; 2012-01-17 10:44:56+0200)
Maven 主页:C:\Apps\apache-maven-3.0.4
Java 版本:1.6.0_35,供应商:Sun Microsystems Inc.
Java home:C:\java\jdk1.6.0_35\jre
默认语言环境:en_GB,平台编码:Cp1252
操作系统名称:“windows xp”,版本:“5.1”,arch:“x86”,家庭:“windows”
插件 POM
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.testing.tester.maven</groupId>
<artifactId>tester</artifactId>
<packaging>maven-plugin</packaging>
<version>1.0-SNAPSHOT</version>
<name>tester Maven Mojo</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-component-annotations</artifactId>
<version>1.5.5</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>1.6</version>
<scope>system</scope>
<systemPath>C:\java\jdk1.6.0_35\lib\tools.jar</systemPath>
<optional>true</optional>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.brsanthu</groupId>
<artifactId>data-exporter</artifactId>
<version>1.0.0</version>
<!-- http://code.google.com/p/data-exporter/wiki/UserGuide -->
<!-- mvn install:install-file -DgroupId=com.brsanthu -DartifactId=data-exporter -Dversion=1.0.0 -Dpackaging=jar -Dfile=http://data-exporter.googlecode.com/files/data-exporter-1.0.0.jar-->
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-component-metadata</artifactId>
<version>1.5.5</version>
<executions>
<execution>
<goals>
<goal>generate-metadata</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
项目 POM
<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.testing.tester</groupId>
<artifactId>tester</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>tester</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.testing.tester.maven</groupId>
<artifactId>tester</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.testing.tester.maven</groupId>
<artifactId>tester</artifactId>
<version>1.0-SNAPSHOT</version>
</plugin>
</plugins>
</build>
</project>