1

I'm using git plugin for Eclipse, as do my daily build, it becomes difficult to track which version of the build from the git is been tested. Unless there is way to auto fetch the branch and commit id from the git plugin and generate a static String version information in the code during each build. Is it possible ?

I prefer not to use a git java API from my code, like say i can fetch the git information with JGit API, if i point it to a local repos, however, that is not how it should be done, as it will create issues with application deployment. Rather should be Eclipse plugin that will generate a string during the build.

3 Answers 3

2

The Eclipse git plugin isn't responsible for the build.

But you can customize your Java builder and add a pre-build event which will modify one of your java files with the right version string in it.

  • See "Pre-Build events in Eclipse" for adding that pre-build step,
  • and use a task like "jgit-buildnumber": a Git "build number" plugin for Maven and Ant based on JGit. That will allow you to get the right version build number string.
Sign up to request clarification or add additional context in comments.

Comments

1

I have made a tool to do what i wanted, inspired from the above pointers by @VonC

GitBuildNumberGenerator

Comments

0

Eclipse creates directory .externalToolBuilders to keep tools settings.

Example of git-version.launch:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<launchConfiguration type="org.eclipse.ui.externaltools.ProgramBuilderLaunchConfigurationType">
    <booleanAttribute key="org.eclipse.debug.ui.ATTR_LAUNCH_IN_BACKGROUND" value="false"/>
    <stringAttribute key="org.eclipse.ui.externaltools.ATTR_LOCATION" value="${workspace_loc:/projX/.externalToolBuilders/git-version.bat}"/>
    <stringAttribute key="org.eclipse.ui.externaltools.ATTR_RUN_BUILD_KINDS" value="full,incremental,"/>
    <booleanAttribute key="org.eclipse.ui.externaltools.ATTR_TRIGGERS_CONFIGURED" value="true"/>
    <stringAttribute key="org.eclipse.ui.externaltools.ATTR_WORKING_DIRECTORY" value="${workspace_loc:/projX}"/>
</launchConfiguration>

and git-version.bat:

@echo off

set VFILE=META-INF/git.properties

git rev-parse --short HEAD >%VFILE%
if ERRORLEVEL 1 (
  set hasgit=no
) else (
  set hasgit=yes
)

rem Git revision.
if %hasgit% == yes (
  set /p rev= <%VFILE%
) else (
  set rev=unknown
)
echo git.rev=%rev% >%VFILE%

rem Are sources has local changes?
if %hasgit% == yes (
  git diff-index --quiet HEAD
  if ERRORLEVEL 1 (
    echo git.clean=no  >>%VFILE%
  ) else (
    echo git.clean=yes >>%VFILE%
  )
)

Build command is registered in .project:

<buildCommand>
    <name>org.eclipse.ui.externaltools.ExternalToolBuilder</name>
    <triggers>full,incremental,</triggers>
    <arguments>
        <dictionary>
            <key>LaunchConfigHandle</key>
            <value>&lt;project&gt;/.externalToolBuilders/git-version.launch</value>
        </dictionary>
    </arguments>
</buildCommand>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.