Java Ninja Chronicles By Norris Shelton

Things I learned in the pursuit of code

We had a need to include information about the current Git version of the files in the build for the purposes of troubleshooting the deploy process.

I stumbled upon the maven Git Commit Id plugin that provides that functionality.

Here is a list of the values that it makes available.

git.build.user.email
git.build.host
git.dirty
git.remote.origin.url
git.closest.tag.name
git.commit.id.describe-short
git.commit.user.email
git.commit.time
git.commit.message.full
git.build.version
git.commit.message.short
git.commit.id.abbrev
git.branch
git.build.user.name
git.closest.tag.commit.count
git.commit.id.describe
git.commit.id
git.tags
git.build.time
git.commit.user.name

These values are made available to replace elements in your specified directory that have the proper key and are surrounded with ${}. An example is.

<constructor-arg index="0" value="${git.commit.id.abbrev}"/>

Here are the steps to using it.

Step 1 – Set the resources directory as modifiable

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
        </resource>
    </resources>
</build>

Step 2 – Include the Git Commit Id plugin

<plugins>
    <plugin>
        <groupId>pl.project13.maven</groupId>
        <artifactId>git-commit-id-plugin</artifactId>
        <version>2.2.0</version>
        <executions>
            <execution>
                <goals>
                    <goal>revision</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <!--
                If you'd like to tell the plugin where your .git directory is,
                use this setting, otherwise we'll perform a search trying to
                figure out the right directory. It's better to add it explicitly IMHO.
            -->
            <dotGitDirectory>${project.basedir}/.git</dotGitDirectory>
        </configuration>
    </plugin>
</plugins>

Additional configuration

To have the plugin generate a file with all of it’s values, instead of replacing values in your source, use the generateGitPropertiesFile configuration. The file will be created in whatever directory you specified in the resources element in Step 1.

<configuration>
    <!-- this is false by default, forces the plugin to generate the git.properties file -->
    <generateGitPropertiesFile>true</generateGitPropertiesFile>
</configuration>

An example of a generated git.properties is below.

#Generated by Git-Commit-Id-Plugin
#Thu Jan 14 13:32:07 EST 2016
git.build.user.email=norris.shelton@java.ninja.com
git.build.host=cdimac0001.java.ninja.com
git.dirty=true
git.remote.origin.url=https\://github.java.ninja.com/my_project.git
git.closest.tag.name=
git.commit.id.describe-short=4b3fea6-dirty
git.commit.user.email=norris.shelton@java.ninja.com
git.commit.time=14.01.2016 @ 11\:19\:21 EST
git.commit.message.full=r18.i1 - US12193 - Fixing package scan in Spring Config files
git.build.version=3.17.0
git.commit.message.short=r18.i1 - US12193 - Fixing package scan in Spring Config files
git.commit.id.abbrev=4b3fea6
git.branch=R18tc
git.build.user.name=Norris Shelton
git.closest.tag.commit.count=
git.commit.id.describe=4b3fea6-dirty
git.commit.id=4b3fea6863da76d37980527b0cc28310f88d7540
git.tags=
git.build.time=14.01.2016 @ 13\:32\:07 EST
git.commit.user.name=Norris.Shelton

By default, the plugin will search for your .git directory. The plugin documentation recommends explicitly setting it. If you are using a multi-module project, you will need to point your dotGitDirectory to a directory above what it expects.

<configuration>
    <!--
        If you'd like to tell the plugin where your .git directory is,
        use this setting, otherwise we'll perform a search trying to
        figure out the right directory. It's better to add it explicitly IMHO.
    -->
    <dotGitDirectory>${project.basedir}/../.git</dotGitDirectory>
</configuration>

To instruct the plugin to work in verbose mode, specify the following configuration.

<configuration>
    <!-- false is default here, it prints some more information during the build -->
    <verbose>true</verbose>
</configuration>

January 15th, 2016

Posted In: Git, GitHub, Java, java ninja, Javaninja, Maven

Leave a Comment

Copying isn’t as straightforward as a copy or a pull request.

  1. Create the new repository – I used the GitHub to create a new repository that was available to others in my organization.
  2. Then push the source repository to the destination repository via the command line. git push –mirror https://github.com/exampleuser/new-repository.git

There you have it. You can then use the GitHub UI and you will see the files from the original project in the new project. At this point, you can clone it, etc and you have ready to code away.

August 18th, 2015

Posted In: Git, GitHub, java ninja, Javaninja

Leave a Comment

When we originally started with Git, we were having trouble with other people being able to use the repository.

9:07:37 AM Push failed
           remote: error: insufficient permission for adding an object to repository database ./objects        
           error: unpack failed: unpack-objects abnormal exit
           error: failed to push some refs to 'norris.shelton@192.168.251.1:/repos/prototype/webservice.git'

How to make a Git repo shareable

git config core.sharedRepository

Look for the following in the output
group or true or 1 or some mask

If not, then run the following (may have to be root)

git config core.sharedRepository group

Then run the following to set the owner, group to whatever you need

sudo chown -R appbuilder:developers *
sudo chmod -R g+rw *

To avoid this problem, type the following to create the repository as a shared repo

git init --shared repository_name.git

November 14th, 2013

Posted In: Git

One Comment

LinkedIn Auto Publish Powered By : XYZScripts.com