简体   繁体   中英

Maven not packing sound resources into JAR

I've been a while trying to figure this out, but to no avail:/

This is my POM part

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>11</source>
                <target>11</target>
            </configuration>
        </plugin>

        <plugin>
            <!-- Build an executable JAR -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.2-beta-5</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>ATComApp.Main</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id> <!-- this is used for inheritance merges -->
                    <phase>package</phase> <!-- bind to the packaging phase -->
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>

    <resources>
        <resource>
            <directory>"src/main/resources"</directory>
            <includes>
                <include>"*/*.wav"</include>
            </includes>
        </resource>
    </resources>
</build>

<dependencies>
    <dependency>
        <groupId>com.googlecode.lanterna</groupId>
        <artifactId>lanterna</artifactId>
        <version>3.0.3</version>
    </dependency>
</dependencies>

and when I build the project it runs but finds no sound:

the code for the sound part is:

@Override
public void run() {
    try {
        File ostFile = new File(Constants.OST_PATH);

        if (!ostFile.exists()) {
            System.out.println("OST File not found.");
            return;
        }

        AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(ostFile);
        Clip clip = AudioSystem.getClip();
        clip.open(audioInputStream);
        clip.loop(Clip.LOOP_CONTINUOUSLY);

    } catch (Exception ex) {
        System.out.println("Sound error.");
        ex.printStackTrace();

When I run with the IDE it runs fine, and when I package it runs but not the sound, prints my string "OST File not found."... when I open the JAR with archive manager the resources folder isn't there.

Does anyone have a clue on why this is happening? Big thanks!

Maven is convention-driven. Ie, you don't need to include the <resources> section in your pom. Just place the WAV files under src/main/resources/YOUR_PACKAGE/ and load them using the class loader.

If

/src/main/java/com/yourorg/MyClass.java
         |
         /resources/com/yourorg/sounds.wav

then you can write (untested):

package com.yourorg;

import ...

public class MyClass {

    @Override
    public void run() {
        try {
            // look up resource in the class path, which resides in the same package
            URL ostFile = MyClass.class.getResource("sounds.wav");

            if (ostFile == null) {
                System.out.println("OST File not found.");
                return;
            }

            AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(ostFile);
            Clip clip = AudioSystem.getClip();
            clip.open(audioInputStream);
            clip.loop(Clip.LOOP_CONTINUOUSLY);

        } catch (Exception ex) {
            System.out.println("Sound error.");
            ex.printStackTrace();
    }
}

If you are trying to identify files within a.jar, you need to use URL , not File . An IDE is able to use File , as all the files are located in visible directories. But once the resource is within a jar, the File command will no longer work.

IDK enough about Maven to comment about why the resource was not jarred. But when I compile programs at the CLI, I have to add a manual step of coping resources into the compiled program's file system. AFAIK, javac does not do this. (There may be CLI options to java that can provide this but I haven't learned them if there are).

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