使用Maven资源:testResources来保持文件权限

29

使用Maven的resources:testResources能否保留文件权限?我的使用场景是将Selenium二进制驱动程序放在/src/test/resources中,我希望能够从我的测试中使用它。但是我的-rwxr-xr-x权限在target/test-classes中被更改为-rw-r--r--

3个回答

38

这似乎是Maven资源插件中的一个错误

如果您正在使用Maven汇编插件,您可以在那里配置文件权限.

如果没有,您可以考虑一种解决方法。您可以通过Ant来完成类似以下的操作:

<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>

谢谢!非常快的回答,解决方法对我来说现在已经足够好了。 - Viktor Hedefalk
1
Maven资源插件仍然“忘记”文件的权限 :-( 除了使用ant,还有更好的方法吗? - Patrick Cornelissen
2
五年过去了,这个漏洞仍未被修复:https://issues.apache.org/jira/browse/MRESOURCES-132 - asmaier
2
更糟糕的是,它已经被错误地关闭-重新打开-关闭了很多次。 - Víctor Romero
4
这个 bug 终于在 2020 年被解决了,maven-resource-plugin 3.2.0+ 的表现符合预期。已经等了很多年终于有修复了! - akudiyar
显示剩余2条评论

3

我添加了一个配置文件,在Unix机器上运行时会自动激活。它执行一个内联的shell脚本,将一个文件夹中所有文件的权限递归地赋予到另一个文件夹同名的文件上(参见SRC和DST变量)。该脚本需要/bin/sh,以及find、xargs和chmod等命令,这些命令应该存在于所有现代系统中。

    <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>

0
我的解决方法是以一种不考虑标志的方式执行脚本。例如,当使用Maven Exec插件运行时,我会使用以下命令:

之前:

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

之后:

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

注意:如果您使用bash -c,即使执行标志关闭,它也会失败。

以下是来自 Maven 创始人之一 Jason van Zyl 的评论:

maven-resources-plugin 从未旨在创建任何将用于裸文件系统的资源。它严格意义上是为了将资源放置到生成的构件中,以便以平台无关的方式使用,通常是从类路径中获取。如果您想要移动归档文件并对其进行解压缩以供使用,我建议使用 assembly 插件制作归档文件,并使用 dependency 插件进行解压缩。

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接