【问题标题】:Ant. Add properties file to jar runtime classpath蚂蚁。将属性文件添加到 jar 运行时类路径
【发布时间】:2014-08-31 14:51:17
【问题描述】:

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

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

Lib 目录包含依赖项 jar,conf 包含属性文件。我将两个目录的内容添加到类路径中。依赖项完美运行,但找不到属性文件。这是我的代码:

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构建文件:

<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 在 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 属性添加到类路径是一种好习惯吗?

【问题讨论】:

    标签: java ant build classpath


    【解决方案1】:

    This thread 处理与您类似的问题。简而言之,您需要采取以下措施来修复它:

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

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

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

    我能够通过这种方法克服您看到的错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-15
      • 2016-04-03
      • 1970-01-01
      相关资源
      最近更新 更多