How do I set directory permissions in maven output?

Sky Kelsey picture Sky Kelsey · Sep 20, 2011 · Viewed 13.6k times · Source

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>

Answer

Northern Pole picture Northern Pole · Jun 3, 2015

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.