How do I set directory permissions in maven output

2019-04-16 06:24发布

问题:

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>

回答1:

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.



回答2:

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>


回答3:

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.