Keep permissions on files with Maven resources:testResources

守給你的承諾、 提交于 2019-12-03 09:30:14
wjans

This seems to be a bug in the Maven Resource Plugin

If you are using the Maven Assembly Plugin, you can configure the file permissions there.

If not, you might consider a workaround. You could do this via Ant by doing something like this:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.6</version>
    <executions>
        <execution>
            <id>process-test-classes</id>
            <phase>process-test-classes</phase>
            <configuration>
                <target>
                    <chmod file="target/test-classes/test.sh" perm="755"/>
                </target>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>

I added a profile that gets activated automatically when run on a Unix machine. It executes an in-line shell script to adopt file permissions from all files in a folder recursively to files of the same name in another folder (see SRC and DST variables). The script requires a /bin/sh as well as find, xargs and chmod, which should exist on all modern systems.

    <profile>
        <id>unix</id>
        <activation>
            <os>
                <family>unix</family>
            </os>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>fix-resource-permissions</id>
                            <goals>
                                <goal>exec</goal>
                            </goals>
                            <phase>process-test-resources</phase>
                            <configuration>
                                <executable>/bin/sh</executable>
                                <arguments>
                                    <argument>-c</argument>
                                    <argument>
                                        set -x

                                        SRC="${basedir}/src/test/resources"
                                        DST="${project.build.directory}/test-classes"

                                        find "$$SRC" -printf "%P\0" | xargs --verbose -0 -I {} chmod --reference="$$SRC/{}" -f "$$DST/{}"
                                    </argument>
                                </arguments>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>

My solution is to execute the scripts in a way that do not mind the flags. So, for instance, when running from Maven Exec Plugin, I use:

Before:

<executable>myScript.sh</executable>
<commandlineArgs>...</commandlineArgs>

After:

<executable>bash</executable>
<commandlineArgs>myScript.sh ...</commandlineArgs>

Note: If you use bash -c, it will also fail if the exec flag is off.


Adding this remarks from Jason van Zyl, one of the creators of Maven:

The maven-resources-plugin was never intended to create any resources that would be used in a naked filesystem. It was strictly intended to place resources into the resultant artifact for use in a platform independent way, in general from the classpath. If you want to move archives around that are going to be unpacked and used I suggest the assembly plugin for making the archives and the dependency plugin for unpacking them.

http://maven.40175.n5.nabble.com/maven-resources-plugin-not-retaining-unix-permissions-td4938002.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!