1

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>
Sky Kelsey
  • 19,192
  • 5
  • 36
  • 77
  • OK, I wasn't able to get the maven-assembly-plugin to set directory permissions properly. I think this is a bug in Maven. The best solution I came up with was adding the exec-maven-plugin and calling chmod manually from that. I have to type "mvn clean package exec:exec" now, but at least the permissions are correct now. – Sky Kelsey Sep 20 '11 at 03:26

3 Answers3

4

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.

Northern Pole
  • 169
  • 2
  • 12
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.

  • Thanks Gary. I'm not setup to test this out yet, but if someone else can verify this works I'll mark it answered. – Sky Kelsey Mar 06 '15 at 18:49
  • A few years ago since this was posted, but I followed this answer and it worked for my case - all folders under output '/' now have the correct permissions as defined in the tags at the pom level. Trying to configure them from within the assembly config file did not work – Bruce Lamond Mar 18 '21 at 04:34
1

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>
amra
  • 16,125
  • 7
  • 50
  • 47
  • Thanks amra. I'll try this out today. – Sky Kelsey Sep 20 '11 at 17:44
  • amra, this isn't a workaround to my problem. I would like to force a specific directory to have a certain set of permissions. This defaultDirectoryMode tag is not allowed inside a fileset tag, and having it as a global behavior for my project is not acceptable. But thank you for the post. – Sky Kelsey Sep 20 '11 at 19:04
  • 1
    By the way, you can just specify those modes in octal with leading zeros, i.e. 0755 and 0644, it's much easier. – dlipofsky Aug 16 '13 at 22:16