Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 298838

Unpack tar.gz to proper path using mvn dependency:unpack

In a Maven build, I would like to download and unpack a tar.gz distribution of keycloak.

Here's my relevant configuration of the maven-dependency-plugin:

<execution>
  <id>unpack-keycloak</id>
  <goals>
    <goal>unpack</goal>
  </goals>
  <phase>generate-resources</phase>
  <configuration>
    <artifactItems>
      <artifactItem>
        <groupId>org.keycloak</groupId>
        <artifactId>keycloak-server-dist</artifactId>
        <version>${keycloak.version}</version>
        <type>tar.gz</type>
        <outputDirectory>/opt/jboss/keycloak</outputDirectory>
        <includes>**/*</includes>
      </artifactItem>
    </artifactItems>
  </configuration>
</execution>

Unfortunately, this results in the archive being unpacked in /opt/jboss/keycloak/keycloak-10.0.1 instead of /opt/jboss/keycloak . Is there any way I can get rid of the intermediate directory?

Obviously I know lots of ways of moving the archive after unpacking, but I'm looking for a solution within this execution, as I have follow-up executions of the same plugin that expect the distribution to already be in place correctly.

Upvotes: 0

Views: 753

Answers (1)

James McPherson
James McPherson

Reputation: 2556

From the docs: https://maven.apache.org/plugins/maven-dependency-plugin/examples/unpacking-filemapper.html

See lines 21-26

Edit from OP:
This is the correct answer, but since the docs are not at all obvious, here's a working configuration

  <execution>
    <id>unpack-keycloak</id>
    <goals>
      <goal>unpack</goal>
    </goals>
    <phase>generate-resources</phase>
    <configuration>
      <artifactItems>
        <artifactItem>
          <groupId>org.keycloak</groupId>
          <artifactId>keycloak-server-dist</artifactId>
          <version>${keycloak.version}</version>
          <type>tar.gz</type>
          <outputDirectory>/opt/jboss/keycloak</outputDirectory>
          <includes>**/*</includes>
          <fileMappers>
            <org.codehaus.plexus.components.io.filemappers.RegExpFileMapper>
              <pattern>^\Qkeycloak-${keycloak.version}/\E</pattern>
              <replacement>./</replacement>
            </org.codehaus.plexus.components.io.filemappers.RegExpFileMapper>
          </fileMappers>
        </artifactItem>
      </artifactItems>
    </configuration>
  </execution>

Upvotes: 1

Related Questions