简体   繁体   中英

how can I reference a local file and include it with my maven assembly when creating an executable jar?

I'm trying to assemble my executable jar, but I don't know how to include a local index.aff and index.dic file with the project when I'm building it. When I execute the jar file, it simply doesn't know where the file is and throws a file-not-found exception. It works fine, during compile time, but when executing my jar file it doesn't seem to be included. Not entirely sure how to fix this. How do I include the local files with my jar without having to bring the external index.* files?

This is my maven command (it builds successfully but without the index files):

$ mvn assembly:assembly -Dfile=src/lib/language-all-4.4.jar -DpomFile=pom.xml -Durl=file:///src/lib/en-US/* -X

public HunSpellChecker(){
    hunspell = Hunspell.getInstance();
    dir = "src/lib/en-US";   //jar doesn't know how to reference this.
    try{
        en = hunspell.getDictionary(dir + "/index" );
    } catch (Exception e){
        System.out.println(e.getMessage());
    }
}

To include files that aren't java code in your jar with Maven you just have to modify the build node in your pom.xml:

    <resources>
        <resource>
            <directory>path/to/the/resources'/directory</directory>
            <includes>
                <include>specific/file</include>
            </includes>
            <excludes>
                <exclude>specific/file</exclude>
            </excludes>
            <targetPath>path/in/jar/where/to/put/resources</targetPath>
        </resource>
    </resources>

By default, all the content of <directory> will be included.
<directory>, <includes>, <excludes> and <targetPath> are all optionals, just use the ones you need. You can find more exemples here .

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