如何在 Maven 输出中设置目录权限?

发布于 2024-12-05 13:11:17 字数 1387 浏览 0 评论 0原文

我正在使用 maven- assembly-plugin 来打包我的构建。

我可以执行一些文件集复制并很好地修改文件权限,但无法修改目录权限。从文档中,我尝试在我关心的目录上使用。但是,无论我指定什么权限,目录总是基于当前的 umask (0022) 创建。 有谁知道在 Maven 构建期间以这种方式修改目录权限的干净方法。唯一有效的是umask 0,但我不想被迫这样做,因为从事该项目的每个人都必须拥有这套。

示例 maven assembly.xml:

<?xml version="1.0"?>
<assembly>
  <id>zip-with-dependencies</id>
  <formats>
    <format>dir</format>
    <format>tar.gz</format>
  </formats>
  <includeBaseDirectory>true</includeBaseDirectory>

  <dependencySets>
    <dependencySet>
      <includes>
        <include>foo:bar</include>
      </includes>
      <outputDirectory>/resources/blah</outputDirectory>
      <useProjectArtifact>true</useProjectArtifact>
      <unpack>true</unpack>
      <scope>runtime</scope>
    </dependencySet>
  </dependencySets>

  <fileSets>
    <fileSet>
      <directory>${basedir}/src/main/web</directory>
      <includes>
        <include>some_dir</include>
      </includes>
      <outputDirectory>web</outputDirectory>
      <fileMode>0777</fileMode>
      <directoryMode>0777</directoryMode>
    </fileSet>
  </fileSets>
</assembly>

I am using the maven-assembly-plugin to package my build.

I am able to perform some copying of file sets and modify file permissions fine, but I am unable to modify directory permissions. From the documentation, I am trying to use on the directories I care about. However, regardless of what permissions I specify, directories are ALWAYS created based off of the current umask (0022).
Does anyone know of a clean way to modify directory permissions in this way during a Maven build. The only thing that works is umask 0, but I would rather not be forced to do this, since everyone working on this project would have to have this set.

Example maven assembly.xml:

<?xml version="1.0"?>
<assembly>
  <id>zip-with-dependencies</id>
  <formats>
    <format>dir</format>
    <format>tar.gz</format>
  </formats>
  <includeBaseDirectory>true</includeBaseDirectory>

  <dependencySets>
    <dependencySet>
      <includes>
        <include>foo:bar</include>
      </includes>
      <outputDirectory>/resources/blah</outputDirectory>
      <useProjectArtifact>true</useProjectArtifact>
      <unpack>true</unpack>
      <scope>runtime</scope>
    </dependencySet>
  </dependencySets>

  <fileSets>
    <fileSet>
      <directory>${basedir}/src/main/web</directory>
      <includes>
        <include>some_dir</include>
      </includes>
      <outputDirectory>web</outputDirectory>
      <fileMode>0777</fileMode>
      <directoryMode>0777</directoryMode>
    </fileSet>
  </fileSets>
</assembly>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

太阳男子 2024-12-12 13:11:17

我也有同样的问题。我测试了上述所有解决方案,但没有一个对我有用。
我想到的对我有用的最佳解决方案是在实际写入这些父文件夹之前将其预先创建为空文件夹。

因此,为了解决原始问题,您应该使用:

<fileSet>
    <directory>./</directory>
    <outputDirectory>/resources</outputDirectory>
    <excludes>
        <exclude>*/**</exclude>
    </excludes>
    <directoryMode>0700</directoryMode>
</fileSet>

这应该放在示例中资源子文件夹的实际副本之前。

./ - 只是一些现有的文件夹。它可以是任何其他文件夹,只要它存在即可。请注意,我们从文件集中排除任何文件。
因此,结果将是一个具有适当权限集的空文件夹。

附带说明一下,无论谁使用 tar 来打包文件,如果没有进行此设置,tar 文件都不会具有为此父文件夹设置的权限。因此,提取将产生一个新文件夹,但具有提取用户及其 umask 的权限。

当然,0700 只是为了示例而使用的。

I had the same problem. I tested all the above solutions and none of them worked for me.
The best solution I had in mind and that worked for me was to pre create these parent folders as empty folders, before actually writing to them.

So, to relate to the original problem, you should use:

<fileSet>
    <directory>./</directory>
    <outputDirectory>/resources</outputDirectory>
    <excludes>
        <exclude>*/**</exclude>
    </excludes>
    <directoryMode>0700</directoryMode>
</fileSet>

This should be put before the actual copy to the subfolder of resources in your example.

./ - is simply some existing folder. It can be any other folder, as long as it exists. Note that we exclude any file from the fileSet.
So the result would be an empty folder with the appropriate set of permissions.

On a side note, whoever uses tar to pack the files, without this set, the tar file won't have the permissions set for this parent folder. So extraction will result with a new folder, but with permissions of the extracting user + his umask.

0700 was used only for the sake of the example, of course.

烛影斜 2024-12-12 13:11:17

我通过结合 pom.xml 和程序集描述符中的设置解决了这个问题。

在 pom 中指定整个 zip 文件的默认值。

        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.2.2</version>

            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>

                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>

            <configuration>
                <descriptors>
                    <descriptor>src/main/assembly/descriptor.xml</descriptor>
                </descriptors>
                <archiverConfig>
                    <directoryMode>0755</directoryMode>
                    <defaultDirectoryMode>0755</defaultDirectoryMode>
                    <fileMode>0644</fileMode>
                </archiverConfig>
            </configuration>
        </plugin>

然后,在程序集描述符中,我为不应具有默认权限的各个文件夹提供覆盖。

<fileSets>
    <fileSet>
        <directory>src/conf</directory>
        <outputDirectory>conf</outputDirectory>
        <fileMode>0600</fileMode>
        <directoryMode>0700</directoryMode>
    </fileSet>
    <fileSet>
        <directory>src/db</directory>
        <outputDirectory>db</outputDirectory>
    </fileSet>
    <fileSet>
        <directory>src/bin</directory>
        <outputDirectory>bin</outputDirectory>
        <fileMode>0755</fileMode>
    </fileSet>

这里,bin 目录中的文件将为所有用户赋予可执行状态。 conf 目录及其中的文件只有所有者才能访问,而 db 目录继承了 pom.xml 中设置的权限。

程序集文件设置在 http://maven.apache.org/ 中描述plugins/maven- assembly-plugin/ assembly.html

除了 amra 识别的 JIRA 问题之外,我找不到任何有关配置部分的官方文档。

I've solved this problem with a combination of settings in the pom.xml and the assembly descriptor.

In pom specify the defaults for the entire zip file.

        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.2.2</version>

            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>

                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>

            <configuration>
                <descriptors>
                    <descriptor>src/main/assembly/descriptor.xml</descriptor>
                </descriptors>
                <archiverConfig>
                    <directoryMode>0755</directoryMode>
                    <defaultDirectoryMode>0755</defaultDirectoryMode>
                    <fileMode>0644</fileMode>
                </archiverConfig>
            </configuration>
        </plugin>

Then in the assembly descriptor I provide the overrides for individual folders that shouldn't have the default permissions.

<fileSets>
    <fileSet>
        <directory>src/conf</directory>
        <outputDirectory>conf</outputDirectory>
        <fileMode>0600</fileMode>
        <directoryMode>0700</directoryMode>
    </fileSet>
    <fileSet>
        <directory>src/db</directory>
        <outputDirectory>db</outputDirectory>
    </fileSet>
    <fileSet>
        <directory>src/bin</directory>
        <outputDirectory>bin</outputDirectory>
        <fileMode>0755</fileMode>
    </fileSet>

Here the files in the bin directory will be given executable state for all users. The conf directory and files in it are accessible only by the owner, and the db directory inherits the permissions from the settings in the pom.

The assembly file settings are described at http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html

I couldn't find any official documentation on the configuration section other than the JIRA issue that amra identified.

静谧 2024-12-12 13:11:17

我发现JIRA问题描述了这种行为。解决方法应该是

<configuration>
    <archiverConfig>
        <fileMode>420</fileMode> <!-- 420(dec) = 644(oct) -->
        <directoryMode>493</directoryMode> <!-- 493(dec) = 755(oct) -->
        <defaultDirectoryMode>493</defaultDirectoryMode>
    </archiverConfig>
</configuration>

I found a JIRA issue describing this behavior. A workaround should be

<configuration>
    <archiverConfig>
        <fileMode>420</fileMode> <!-- 420(dec) = 644(oct) -->
        <directoryMode>493</directoryMode> <!-- 493(dec) = 755(oct) -->
        <defaultDirectoryMode>493</defaultDirectoryMode>
    </archiverConfig>
</configuration>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文