读取config.xml文件时无法执行目标org.codehaus.mojo exec-maven-plugin

时间:2014-02-01 21:08:33

标签: maven exec-maven-plugin

当我在eclipse中将其作为java应用程序运行时,我的代码正在运行,但是当我使用maven-exec插件运行maven install时它失败了。我知道它失败了,因为它找不到配置文件但是怎么样?

如果我排除maven-exec插件,它会成功构建,我可以执行带有依赖项的jar文件。

public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "... starting DomParser" );
        System.out.println( "... inside app" );

        DOMParser.loadXML("/config.xml");

        System.out.println( "... ending DomParser" );
    }
}

这是简化的DomParser类

static SAXBuilder builder = new SAXBuilder();
static Document doc;
public static void loadXML(String path) {
    java.net.URL url = DOMParser.class.getClass().getResource(path);
    doc = builder.build(url);
    root = doc.getRootElement();
}

这是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.ericsson.ci.simnet</groupId>
    <artifactId>domparser</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>domparser</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>4.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.14</version>
        </dependency>

        <dependency>
            <groupId>org.jdom</groupId>
            <artifactId>jdom2</artifactId>
            <version>2.0.5</version>
        </dependency>

    </dependencies>

    <build>

        <!-- <finalName>${project.name}</finalName> -->

        <plugins>
            <!-- Tell maven to compile using Java 1.7 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <executions>
                    <execution>
                        <phase>test</phase>
                        <goals>
                            <goal>java</goal>
                        </goals>
                        <configuration>
                            <mainClass>com.x.ci.simnet.domparser.App</mainClass>
                            <arguments>
                                <argument></argument>
                            </arguments>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- to bundle necessary classes of dependent JAR files -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>1.5</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.x.ci.simnet.domparser.App</mainClass>
                                </transformer>
                            </transformers>
                            <!--<shadedArtifactAttached>true</shadedArtifactAttached> -->
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

错误在这里:

[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:java (default) on project domparser: An exception occured while executing the Java class. null: InvocationTargetException: NullPointerException -> [Help 1]

1 个答案:

答案 0 :(得分:1)

晚上很长时间后我找到了解决方案。感谢大家帮忙。

我替换了我的DomParser类代码,如下所示

import org.jdom2.input.SAXBuilder;
import org.jdom2.Document;

public class DOMParser {

static SAXBuilder builder = new SAXBuilder();
static Document doc;
...
...

public static void loadXML(String path) {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    java.net.URL url = classLoader.getResource(path);
    doc = builder.build(url);
    root = doc.getRootElement();
}

因此,关键技巧是使用以下代码行。

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

这允许您按照以下方式运行应用程序

  1. Eclipse&gt;作为java应用程序运行
  2. maven exec plugin
  3. 带有maven阴影插头的可执行jar
  4. 我希望这有助于其他人将来也有类似的问题。

    更新:

    我忘了提到我也略微修改了App类代码。只需一行。我传递文件anme而没有正斜杠,如下所示。

    DOMParser.loadXML("config.xml");