Maven简单天气教程

时间:2013-05-30 09:10:42

标签: maven-3

我正在关注Maven by Example中的简单天气教程。当我执行程序时,我得到以下异常。这需要任何特定的类路径设置吗?

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.example.maven.weather</groupId>
<artifactId>simple-weather</artifactId>
<version>1.0</version>
<packaging>jar</packaging>

<name>simple-weather</name>
<url>http://maven.apache.org</url>

<licenses>
    <license>
        <name>Apache 2</name>
        <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
        <distribution>repo</distribution>
        <comments>A business-friendly OSS license</comments>
    </license>
</licenses>

<organization>
    <name>Sonatype</name>
    <url>http://www.sonatype.com</url>
</organization>

<developers>
    <developer>
        <id>jason</id>
        <name>Jason Van Zyl</name>
        <email>jason@maven.org</email>
        <url>http://www.sonatype.com</url>
        <organization>Sonatype</organization>
        <organizationUrl>http://www.sonatype.com</organizationUrl>
        <roles>
            <role>developer</role>
        </roles>
        <timezone>-6</timezone>
    </developer>
</developers>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.14</version>
    </dependency>
    <dependency>
        <groupId>dom4j</groupId>
        <artifactId>dom4j</artifactId>
        <version>1.6.1</version>
    </dependency>
    <dependency>
        <groupId>jaxen</groupId>
        <artifactId>jaxen</artifactId>
        <version>1.1.1</version>
    </dependency>
    <dependency>
        <groupId>velocity</groupId>
        <artifactId>velocity</artifactId>
        <version>1.5</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>3.8.1</version>
  <scope>test</scope>
</dependency>

Main.java

package com.example.maven.weather;

import java.io.InputStream;

import org.apache.log4j.PropertyConfigurator;

public class Main {

public static void main(String[] args) throws Exception {
    // Configure Log4J
    PropertyConfigurator.configure(Main.class.getClassLoader()
                                   .getResource("log4j.properties"));

    // Read the Zip Code from the Command-line (if none supplied, use 60202)
    String zipcode = "60202";
    try {
        zipcode = args[0];
    } catch( Exception e ) {}

    // Start the program
    new Main(zipcode).start();
}

private String zip;

public Main(String zip) {
    this.zip = zip;
}

public void start() throws Exception {
    // Retrieve Data
    InputStream dataIn = new YahooRetriever().retrieve( zip );

    // Parse Data
    Weather weather = new YahooParser().parse( dataIn );

    // Format (Print) Data
    System.out.print( new WeatherFormatter().format( weather ) );
}
}  

执行命令

mvn exec:java -Dexec.mainClass=com.exa
mple.maven.weather.Main
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building simple-weather 1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> exec-maven-plugin:1.2.1:java (default-cli) @ simple-weather >>>
[INFO]
[INFO] <<< exec-maven-plugin:1.2.1:java (default-cli) @ simple-weather <<<
[INFO]
[INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ simple-weather ---
[WARNING]
java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:297)
        at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.NullPointerException
        at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurato
r.java:433)
        at org.apache.log4j.PropertyConfigurator.configure(PropertyConfigurator.
java:336)
        at com.example.maven.weather.Main.main(Main.java:12)
        ... 6 more
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.902s
[INFO] Finished at: Thu May 30 14:23:06 IST 2013
[INFO] Final Memory: 4M/15M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:java (d
efault-cli) on project simple-weather: An exception occured while executing the
Java class. null: InvocationTargetException: NullPointerException -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e swit
ch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please rea
d the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionE
xception

2 个答案:

答案 0 :(得分:0)

您的应用程序似乎无法找到log4j.properties 你使用标准的maven项目结构吗? log4j.properties位于何处?

答案 1 :(得分:0)

'resources'文件夹应位于主文件夹(src / main / resources)中,而不是src / resources。

相关问题