Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven corrupting binary files in src/main/resources when building jar

Tags:

java

maven

dll

I've got an issue with a maven project where I am distributing dlls from the src/main/resources/lib folder.

The project is built as a single jar with dependencies using the maven-assembly-plugin.

Unfortunately, the maven process is corrupting my dll libraries during the copy process so that they are no longer useful to the application.

I've had a look at such concepts as resource filtering.

Here's my relevant pom.xml

Does anyone have any ideas?

I think I need to do something like this but so far it's not working for me.

            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>

                    <resources>
                        <resource>
                            <directory>${basedir}/src/main/resources/lib</directory>
                            <filtering>false</filtering>
                        </resource>
                    </resources>

                    <archive>
                        <manifest>
                            <mainClass>de.bochumuniruhr.psy.bio.behaviourcoder.Main</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>

My final solution (based on the answers below):

Thank you for the great answers. I ended up going with the answer that doesn't require extending the configuration of the maven-resources-plugin. I placed my binary files in the src/main/resources/unfiltered-resources folder as I needed to filter my other resources.

Here is a link to the source code.

Below is my final working pom at the time of writing.

<build>
        <finalName>BehaviourCoder_${git.build.time}_${git.commit.id.describe-short}</finalName>
        <resources>
            <resource>
                <directory>${basedir}/src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>${basedir}/src/main/unfiltered-resources</directory>
                <filtering>false</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>pl.project13.maven</groupId>
                <artifactId>git-commit-id-plugin</artifactId>
                <version>2.2.2</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>revision</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <generateGitPropertiesFile>true</generateGitPropertiesFile>
                    <injectAllReactorProjects>true</injectAllReactorProjects>
                    <dateFormat>yyyy-MM-dd_HHmmss</dateFormat>
                    <dateFormatTimeZone>UTC</dateFormatTimeZone>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>de.bochumuniruhr.psy.bio.behaviourcoder.Main</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
        </plugins>
    </build>
like image 375
John Deverall Avatar asked Oct 13 '17 17:10

John Deverall


2 Answers

This part:

                <resources>
                    <resource>
                        <directory>${basedir}/src/main/resources/lib</directory>
                        <filtering>false</filtering>
                    </resource>
                </resources>

Should be under under the <build/> section like this:

 <project>
      <build>
          <resources>
              <resource>
                  <directory>${basedir}/src/main/resources/lib</directory>
                  <filtering>false</filtering>
              </resource>
          </resources>
          <plugins>
              ...
          </plugins>
      </build>
 </project>
like image 57
carlspring Avatar answered Nov 10 '22 10:11

carlspring


When assembly plugin kicks in it is already too late, as the resources were already copied by maven resources plugin. You should exclude filtering on earlier phase (when the resources are being copied to target folder by maven resource plugin).

See maven's docs how to do this: https://maven.apache.org/plugins/maven-resources-plugin/examples/binaries-filtering.html

For your case this can be something like:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>3.0.2</version>
            <configuration>
                <nonFilteredFileExtensions>
                    <nonFilteredFileExtension>dll</nonFilteredFileExtension>
                </nonFilteredFileExtensions>
            </configuration>
        </plugin>
    </plugins>
like image 5
walkeros Avatar answered Nov 10 '22 11:11

walkeros