简体   繁体   中英

How to externalize XML files when exporting spring batch to JAR

So I have a program that I exported to a jar. It's a spring project that repeatedly writes information from a database to text files. There are four java files:

JAVA FILES
    App.java
    Runscheduler.java
    Rowmapper.java

    pojoObject.java

And two XML files

XML files
    job-request.xml
    database.xml

As well as two configuration files

CONF
    log4j.properties
    configuration.properties

I have a batch file run the jar and everything works just fine. The only files I have to externalize are my properties files.

So here's where the problem lies.

When I package the spring project from eclipse into an executable jar and then run it through a batch (.bat) file, it packages everything except the "CONF" folder; and I have to put the "CONF" folder in the same folder as the .bat and the .jar file. Then it runs perfectly. Right now I'm trying to externalize the two spring XML files as well, but I don't know how since they're packaged with the .jar. The App.java (which contains the "main" function) loads the two xml files like this:

String[] springConfig  = 
        {   "spring/batch/config/database.xml", 
            "spring/batch/jobs/job-request.xml" 
        };

ApplicationContext context = 
            new ClassPathXmlApplicationContext(springConfig);

But the actual path is

resources/spring/batch/config/database.xml
resources/spring/batch/jobs/job-request.xml

Inside the project directory. However when I try to change the path that the springConfig loads to resources/spring...it doesn't work.

If there's any way to package the XML files, especially job-request, outside of the .jar and still be able to run the program, please let me know. Thank you!

EDIT: For anyone wondering, I solved the problem by simply exporting as a file system. Worked like a charm.

We had a similar situation. we used ant jar target with "excludes" to exclude the xml files that were not required in the jar.

<jar jarfile="${dir.dist}/ourjar.jar" 
         basedir="${dir.build}"
         includes="**/*.class"
         excludes="..."/>

We accessed the external xml files as follows

public class BootstrapBean {
   private File configFile;
   public void setConfigFile(Resource resource) {
      this.configFile = resource.getFile()
   }
  ...
}

The XML configuration was

<bean id="bean" class="mypackage.BootstrapBean">
   <property name="file" value="file: a path to the xml file"/>
</bean>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM