Rainer Jung Rainer Jung - 3 months ago 23
Java Question

Get UID in a maven variable?

I can the the current username in a

pom.xml
-file with
${user.name}
, but is there a way to get the uid (userid)?

I use
exec-maven-plugin
and add arguments to a execution. I've tried different things (
${env.UID}
,
${user.id}
,
$UID
), but neither work.

I need it to start a process in docker that writes files to a shared directory. If I don't start with the
-u
-parameter all files will belong to
root
.

The configuration is like:

<configuration>
<executable>docker</executable>
<arguments>
<argument>run</argument>
<argument>-u</argument>
<argument>$UID</argument>
<argument>...</argument>
</arguments>
</configuration>

Answer

I intended to write a maven plugin to provide the property user.id but then I did not find a way to get the userid with standard java methods.
There are two projects (Java Native Access, Java Native Interface) to have direct access to native informations, but so far I haven't found a way to get the UID.

I fixed my problem by adding a umask-call in my Dockerfile. I strongly recomend to use a solution that does not requires the user-id within maven.

But working on the issue I created a solution for others that really need this. It takes the user.name-property from the system-properties and then opens the passwd-file to search for the uid. Of course this will only work for unix-like systems and it couples the build directly with your environment, so use this solution wisely (idealy not at all).
To use my solution, add this to your pom.xml:

<plugin>
  <!-- Plugin only works with unix-like environments, please use it wisely! -->
  <groupId>org.rjung.util</groupId>
  <artifactId>user-id-maven-plugin</artifactId>
  <version>1.0</version>
  <executions>
    <execution>
      <goals>
        <goal>user-id</goal>
      </goals>
    </execution>
  </executions>
</plugin>