蚂蚁。将属性文件添加到jar运行时类路径

时间:2014-07-10 09:31:38

标签: java ant build classpath

我正在尝试构建一个简单的应用程序。该应用程序包含一个可运行的jar和两个文件夹:lib和conf。我想最终得到这样的结构:

/app  
------/conf/main.props  
------/lib/*.jar  
------App.jar  

Lib dir包含依赖项jar,conf包含属性文件。我将两个目录的内容添加到classpath。依赖关系完美地工作,但找不到属性文件。这是我的代码:

package com.example;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.util.Properties;

public class HelloAnt {
  public static void main(String[] args) throws IOException {
    BasicConfigurator.configure();
    Logger logger = Logger.getLogger(HelloAnt.class);
    logger.warn("Hello from ant!");
    Properties props = new Properties();
    props.load(ClassLoader.getSystemResourceAsStream("conf/main.props"));
    logger.warn("Prop is: " + props.get("name"));
    HttpClient client = HttpClients.createDefault();
    HttpGet get = new HttpGet("https://www.google.co.uk");
    try {
        HttpResponse response = client.execute(get);
        String s = EntityUtils.toString(response.getEntity());
        System.out.println(s);
    } catch (IOException e) {
        e.printStackTrace();
    }
 }
}

和ant build文件:

<project name="HelloWorld" basedir="." default="compile">
<property name="lib.dir" location="lib"/>
<property name="src.dir" location="src/main/java"/>
<property name="build.dir" location="build"/>
<property name="classes.dir" location="${build.dir}/classes"/>
<property name="app.dir" location="${build.dir}/hello-ant"/>
<property name="app.lib.dir" location="${app.dir}/lib"/>
<property name="app.conf.dir" location="${app.dir}/conf"/>
<property name="app.jar" location="${app.dir}/${ant.project.name}.jar"/>
<property name="file.jar" location="${app.dir}/${ant.project.name}.jar"/>
<property name="main.class" value="com.example.HelloAnt"/>

<target name="clean">
    <delete dir="${build.dir}"/>
</target>

<target name="init">
    <mkdir dir="${build.dir}"/>
</target>

<path id="compile.classpath">
    <fileset dir="lib">
        <include name="*.jar"/>
    </fileset>
    <fileset dir="src/main/resources">
        <include name="*.props"/>
    </fileset>
</path>

<target name="compile" depends="init">
    <mkdir dir="${classes.dir}"/>
    <javac srcdir="${src.dir}" destdir="${classes.dir}">
        <classpath refid="compile.classpath"/>
    </javac>
</target>


<target name="jar" depends="compile">
    <mkdir dir="${app.dir}"/>
    <mkdir dir="${app.lib.dir}"/>
    <mkdir dir="${app.conf.dir}"/>
    <copy todir="${app.conf.dir}">
        <fileset dir="src/main/resources">
            <include name="*.props"/>
        </fileset>
    </copy>
    <echo message="compile.classpath"/>
    <copy todir="${app.lib.dir}">
        <fileset dir="${lib.dir}" includes="*.jar"/>
    </copy>
    <path id="runtime.classpath">
        <fileset dir="${app.lib.dir}">
            <include name="*.jar"/>
        </fileset>
    </path>
    <pathconvert property="manifest.classpath" pathsep=" ">
        <path refid="compile.classpath"/>
        <mapper>
            <chainedmapper>
                <flattenmapper/>
                <globmapper from="*.jar" to="lib/*.jar"/>
            </chainedmapper>
            <chainedmapper>
                <flattenmapper/>
                <globmapper from="*.props" to="conf/*.props"/>
            </chainedmapper>
        </mapper>
    </pathconvert>

    <jar destfile="${file.jar}" basedir="${classes.dir}">
        <manifest>
            <attribute name="Main-Class" value="${main.class}"/>
            <attribute name="Class-Path" value="${manifest.classpath}"/>
        </manifest>
    </jar>
</target>
</project>

输出是:

  

线程“main”java.lang.NullPointerException中的异常       at java.util.Properties $ LineReader.readLine(Properties.java:434)       在java.util.Properties.load0(Properties.java:353)       在java.util.Properties.load(Properties.java:341)       在com.example.HelloAnt.main(未知来源)   0 [main] WARN com.example.HelloAnt - 来自蚂蚁的你好!

Nullpointer因空输入流而无法使用。那么我做错了什么?将jar属性的外部添加到classpath是一个好习惯吗?

1 个答案:

答案 0 :(得分:2)

This thread处理与您类似的问题。简而言之,您需要做的就是修复它:

在MANIFEST.MF文件的Class-Path标头中,您需要指定conf/而不是conf/main.props。要快速解决问题,只需将映射器更改为以下内容:

   <chainedmapper>
       <flattenmapper/>
       <globmapper from="*.props" to="conf/"/>
   </chainedmapper>

然后在您的代码中,您应该加载属性文件,如下所示:HelloAnt.class.getResourceAsStream("/main.props")

我能够克服您使用此方法看到的错误。