Failed to execute goal org.codehaus.mojo exec-maven-plugin while reading a config.xml file

StackOverflow https://stackoverflow.com/questions/21503691

  •  05-10-2022
  •  | 
  •  

Domanda

My code is working when I run it as java application in eclipse but it fails when I run maven install with maven-exec plug-in. I know that it it fails because of it doesn't find config file but how?

If I exclude the maven-exec plugin it builds successfully and I can execute my jar file with dependencies.

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" );
    }
}

Here is the simplified DomParser class

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();
}

This is the 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 is here:

[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]
È stato utile?

Soluzione

After long hours at night I found the solution. Thanks to everybody for helping out.

I replaced my DomParser class code as below

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();
}

So, the key trick was to use the following code line.

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

This allows you to run your application as following ways

  1. Eclipse > Run as java application
  2. maven exec plugin
  3. executable jar with maven shade plug in

I hope this help other people have similar issue in future.

UPDATE:

I forgot to mention that I also slightly modified the App class code. Just one line. I pass file anme without forward slash as below.

DOMParser.loadXML("config.xml"); 
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top