diff --git a/common/pom.xml b/common/pom.xml
new file mode 100644
index 00000000..b36b4dcb
--- /dev/null
+++ b/common/pom.xml
@@ -0,0 +1,38 @@
+
+ 4.0.0
+
+
+ pl.project13.maven
+ git-commit-id-plugin-parent
+ 2.1.10-SNAPSHOT
+
+
+ pl.project13.maven
+ git-commit-id-plugin-common
+ Git Commit Id Plugin Maven Common
+
+
+
+
+
+ org.eclipse.jgit
+ org.eclipse.jgit
+
+
+
+
+ com.google.guava
+ guava
+
+
+ com.google.code.findbugs
+ jsr305
+
+
+
+
+ joda-time
+ joda-time
+
+
+
\ No newline at end of file
diff --git a/src/main/java/pl/project13/jgit/DescribeCommand.java b/common/src/main/java/pl/project13/jgit/DescribeCommand.java
similarity index 95%
rename from src/main/java/pl/project13/jgit/DescribeCommand.java
rename to common/src/main/java/pl/project13/jgit/DescribeCommand.java
index bd7931b3..abe7b8af 100644
--- a/src/main/java/pl/project13/jgit/DescribeCommand.java
+++ b/common/src/main/java/pl/project13/jgit/DescribeCommand.java
@@ -17,12 +17,24 @@
package pl.project13.jgit;
-import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Function;
-import com.google.common.base.Optional;
-import com.google.common.base.Preconditions;
-import com.google.common.base.Throwables;
-import com.google.common.collect.Lists;
+import static com.google.common.collect.Lists.newArrayList;
+import static com.google.common.collect.Lists.newLinkedList;
+import static com.google.common.collect.Maps.newHashMap;
+import static com.google.common.collect.Sets.newHashSet;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Queue;
+import java.util.Set;
+import java.util.regex.Pattern;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.GitCommand;
import org.eclipse.jgit.api.Status;
@@ -35,22 +47,19 @@
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevTag;
import org.eclipse.jgit.revwalk.RevWalk;
-import org.jetbrains.annotations.NotNull;
-import org.jetbrains.annotations.Nullable;
+
import pl.project13.jgit.dummy.DatedRevTag;
import pl.project13.maven.git.GitDescribeConfig;
import pl.project13.maven.git.log.LoggerBridge;
import pl.project13.maven.git.log.StdOutLoggerBridge;
import pl.project13.maven.git.util.Pair;
-import java.io.IOException;
-import java.util.*;
-import java.util.regex.Pattern;
-
-import static com.google.common.collect.Lists.newArrayList;
-import static com.google.common.collect.Lists.newLinkedList;
-import static com.google.common.collect.Maps.newHashMap;
-import static com.google.common.collect.Sets.newHashSet;
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Function;
+import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
+import com.google.common.base.Throwables;
+import com.google.common.collect.Lists;
/**
* Implements git's
describe
command.
@@ -115,7 +124,7 @@ public class DescribeCommand extends GitCommand {
*
* @param repo the {@link org.eclipse.jgit.lib.Repository} this command should interact with
*/
- @NotNull
+ @Nonnull
public static DescribeCommand on(Repository repo) {
return new DescribeCommand(repo);
}
@@ -139,13 +148,13 @@ private void initDefaultLoggerBridge(boolean verbose) {
loggerBridge = new StdOutLoggerBridge(verbose);
}
- @NotNull
+ @Nonnull
public DescribeCommand setVerbose(boolean verbose) {
loggerBridge.setVerbose(verbose);
return this;
}
- @NotNull
+ @Nonnull
public DescribeCommand withLoggerBridge(LoggerBridge bridge) {
this.loggerBridge = bridge;
return this;
@@ -158,7 +167,7 @@ public DescribeCommand withLoggerBridge(LoggerBridge bridge) {
*
* true
by default.
*/
- @NotNull
+ @Nonnull
public DescribeCommand always(boolean always) {
this.alwaysFlag = always;
log("--always =", always);
@@ -176,7 +185,7 @@ public DescribeCommand always(boolean always) {
*
* false
by default.
*/
- @NotNull
+ @Nonnull
public DescribeCommand forceLongFormat(@Nullable Boolean forceLongFormat) {
if (forceLongFormat != null) {
this.forceLongFormat = forceLongFormat;
@@ -193,7 +202,7 @@ public DescribeCommand forceLongFormat(@Nullable Boolean forceLongFormat) {
*
* An of 0 will suppress long format, only showing the closest tag.
*/
- @NotNull
+ @Nonnull
public DescribeCommand abbrev(@Nullable Integer n) {
if (n != null) {
Preconditions.checkArgument(n < 41, String.format("N (commit abbres length) must be < 41. (Was:[%s])", n));
@@ -232,7 +241,7 @@ public DescribeCommand abbrev(@Nullable Integer n) {
* tags to be included in the search, enable this option.
*
*/
- @NotNull
+ @Nonnull
public DescribeCommand tags(@Nullable Boolean includeLightweightTagsInSearch) {
if (includeLightweightTagsInSearch != null) {
tagsFlag = includeLightweightTagsInSearch;
@@ -254,7 +263,7 @@ public DescribeCommand tags() {
*
* @return itself, after applying the settings
*/
- @NotNull
+ @Nonnull
public DescribeCommand apply(@Nullable GitDescribeConfig config) {
if (config != null) {
always(config.isAlways());
@@ -275,7 +284,7 @@ public DescribeCommand apply(@Nullable GitDescribeConfig config) {
* @param dirtyMarker the marker name to be appended to the describe output when the workspace is dirty
* @return itself, to allow fluent configuration
*/
- @NotNull
+ @Nonnull
public DescribeCommand dirty(@Nullable String dirtyMarker) {
Optional option = Optional.fromNullable(dirtyMarker);
log("--dirty =", option.or(""));
@@ -290,7 +299,7 @@ public DescribeCommand dirty(@Nullable String dirtyMarker) {
* @param pattern the glob style pattern to match against the tag names
* @return itself, to allow fluent configuration
*/
- @NotNull
+ @Nonnull
public DescribeCommand match(@Nullable String pattern) {
matchOption = Optional.fromNullable(pattern);
log("--match =", matchOption.or(""));
@@ -367,7 +376,7 @@ private DescribeResult createDescribeResult(ObjectReader objectReader, ObjectId
}
}
- private static boolean foundZeroTags(@NotNull Map> tags) {
+ private static boolean foundZeroTags(@Nonnull Map> tags) {
return tags.isEmpty();
}
@@ -392,11 +401,11 @@ boolean findDirtyState(Repository repo) throws GitAPIException {
}
@VisibleForTesting
- static boolean hasTags(ObjectId headCommit, @NotNull Map> tagObjectIdToName) {
+ static boolean hasTags(ObjectId headCommit, @Nonnull Map> tagObjectIdToName) {
return tagObjectIdToName.containsKey(headCommit);
}
- RevCommit findHeadObjectId(@NotNull Repository repo) throws RuntimeException {
+ RevCommit findHeadObjectId(@Nonnull Repository repo) throws RuntimeException {
try {
ObjectId headId = repo.resolve("HEAD");
@@ -411,7 +420,7 @@ RevCommit findHeadObjectId(@NotNull Repository repo) throws RuntimeException {
}
}
- private List findCommitsUntilSomeTag(Repository repo, RevCommit head, @NotNull Map> tagObjectIdToName) {
+ private List findCommitsUntilSomeTag(Repository repo, RevCommit head, @Nonnull Map> tagObjectIdToName) {
RevWalk revWalk = new RevWalk(repo);
try {
revWalk.markStart(head);
@@ -438,7 +447,7 @@ private List findCommitsUntilSomeTag(Repository repo, RevCommit head,
* @return distance (number of commits) between the given commits
* @see mdonoughe/jgit-describe/blob/master/src/org/mdonoughe/JGitDescribeTask.java
*/
- private int distanceBetween(@NotNull Repository repo, @NotNull RevCommit child, @NotNull RevCommit parent) {
+ private int distanceBetween(@Nonnull Repository repo, @Nonnull RevCommit child, @Nonnull RevCommit parent) {
RevWalk revWalk = new RevWalk(repo);
try {
@@ -491,7 +500,7 @@ private int distanceBetween(@NotNull Repository repo, @NotNull RevCommit child,
}
}
- private static void seeAllParents(@NotNull RevWalk revWalk, RevCommit child, @NotNull Set seen) throws IOException {
+ private static void seeAllParents(@Nonnull RevWalk revWalk, RevCommit child, @Nonnull Set seen) throws IOException {
Queue q = newLinkedList();
q.add(child);
@@ -508,7 +517,7 @@ private static void seeAllParents(@NotNull RevWalk revWalk, RevCommit child, @No
}
// git commit id -> its tag (or tags)
- private Map> findTagObjectIds(@NotNull Repository repo, boolean tagsFlag) {
+ private Map> findTagObjectIds(@Nonnull Repository repo, boolean tagsFlag) {
Map> commitIdsToTags = newHashMap();
RevWalk walk = new RevWalk(repo);
@@ -626,7 +635,7 @@ private String createMatchPattern() {
}
@VisibleForTesting
- static String trimFullTagName(@NotNull String tagName) {
+ static String trimFullTagName(@Nonnull String tagName) {
return tagName.replaceFirst("refs/tags/", "");
}
diff --git a/src/main/java/pl/project13/jgit/DescribeResult.java b/common/src/main/java/pl/project13/jgit/DescribeResult.java
similarity index 91%
rename from src/main/java/pl/project13/jgit/DescribeResult.java
rename to common/src/main/java/pl/project13/jgit/DescribeResult.java
index 8817a8df..7cf37865 100644
--- a/src/main/java/pl/project13/jgit/DescribeResult.java
+++ b/common/src/main/java/pl/project13/jgit/DescribeResult.java
@@ -17,19 +17,21 @@
package pl.project13.jgit;
-import com.google.common.base.Joiner;
-import com.google.common.base.Optional;
-import com.google.common.base.Preconditions;
-import org.eclipse.jgit.lib.AbbreviatedObjectId;
-import org.eclipse.jgit.lib.ObjectId;
-import org.eclipse.jgit.lib.ObjectReader;
-import org.jetbrains.annotations.NotNull;
-import org.jetbrains.annotations.Nullable;
+import static com.google.common.collect.Lists.newArrayList;
import java.io.IOException;
import java.util.List;
-import static com.google.common.collect.Lists.newArrayList;
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
+import org.eclipse.jgit.lib.AbbreviatedObjectId;
+import org.eclipse.jgit.lib.ObjectId;
+import org.eclipse.jgit.lib.ObjectReader;
+
+import com.google.common.base.Joiner;
+import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
/**
* Represents the result of a git describe
command.
@@ -53,32 +55,32 @@ public class DescribeResult {
public static final DescribeResult EMPTY = new DescribeResult("");
- public DescribeResult(@NotNull String tagName) {
+ public DescribeResult(@Nonnull String tagName) {
this(tagName, false, Optional.absent());
}
- public DescribeResult(@NotNull ObjectReader objectReader, String tagName, int commitsAwayFromTag, @Nullable ObjectId commitId) {
+ public DescribeResult(@Nonnull ObjectReader objectReader, String tagName, int commitsAwayFromTag, @Nullable ObjectId commitId) {
this(objectReader, tagName, commitsAwayFromTag, commitId, false, Optional.absent());
}
- public DescribeResult(@NotNull ObjectReader objectReader, @NotNull ObjectId commitId) {
+ public DescribeResult(@Nonnull ObjectReader objectReader, @Nonnull ObjectId commitId) {
this.objectReader = objectReader;
this.commitId = Optional.of(commitId);
this.abbreviatedObjectId = createAbbreviatedCommitId(objectReader, commitId, this.abbrev);
}
- public DescribeResult(@NotNull ObjectReader objectReader, String tagName, int commitsAwayFromTag, ObjectId commitId, boolean dirty, String dirtyMarker) {
+ public DescribeResult(@Nonnull ObjectReader objectReader, String tagName, int commitsAwayFromTag, ObjectId commitId, boolean dirty, String dirtyMarker) {
this(objectReader, tagName, commitsAwayFromTag, commitId, dirty, Optional.of(dirtyMarker));
}
- public DescribeResult(@NotNull ObjectReader objectReader, String tagName, int commitsAwayFromTag, ObjectId commitId, boolean dirty, Optional dirtyMarker) {
+ public DescribeResult(@Nonnull ObjectReader objectReader, String tagName, int commitsAwayFromTag, ObjectId commitId, boolean dirty, Optional dirtyMarker) {
this(objectReader, commitId, dirty, dirtyMarker);
this.tagName = Optional.of(tagName);
this.commitsAwayFromTag = commitsAwayFromTag;
}
- public DescribeResult(@NotNull ObjectReader objectReader, @NotNull ObjectId commitId, boolean dirty, @NotNull Optional dirtyMarker) {
+ public DescribeResult(@Nonnull ObjectReader objectReader, @Nonnull ObjectId commitId, boolean dirty, @Nonnull Optional dirtyMarker) {
this.objectReader = objectReader;
this.commitId = Optional.of(commitId);
@@ -88,13 +90,13 @@ public DescribeResult(@NotNull ObjectReader objectReader, @NotNull ObjectId comm
this.dirtyMarker = dirtyMarker.or("");
}
- public DescribeResult(@NotNull String tagName, boolean dirty, @NotNull Optional dirtyMarker) {
+ public DescribeResult(@Nonnull String tagName, boolean dirty, @Nonnull Optional dirtyMarker) {
this.tagName = Optional.of(tagName);
this.dirty = dirty;
this.dirtyMarker = dirtyMarker.or("");
}
- @NotNull
+ @Nonnull
public DescribeResult withCommitIdAbbrev(int n) {
Preconditions.checkArgument(n >= 0, String.format("The --abbrev parameter must be >= 0, but it was: [%s]", n));
this.abbrev = n;
@@ -202,7 +204,7 @@ private String gPrefixedCommitId(String name) {
*
* @return the abbreviated commit id, possibly longer than the requested len (if it wouldn't be unique)
*/
- private static Optional createAbbreviatedCommitId(@NotNull ObjectReader objectReader, ObjectId commitId, int requestedLenght) {
+ private static Optional createAbbreviatedCommitId(@Nonnull ObjectReader objectReader, ObjectId commitId, int requestedLenght) {
if(requestedLenght < 2) {
// 0 means we don't want to print commit id's at all
return Optional.absent();
diff --git a/src/main/java/pl/project13/jgit/dummy/DatedRevTag.java b/common/src/main/java/pl/project13/jgit/dummy/DatedRevTag.java
similarity index 100%
rename from src/main/java/pl/project13/jgit/dummy/DatedRevTag.java
rename to common/src/main/java/pl/project13/jgit/dummy/DatedRevTag.java
diff --git a/common/src/main/java/pl/project13/maven/git/GitDataLoader.java b/common/src/main/java/pl/project13/maven/git/GitDataLoader.java
new file mode 100644
index 00000000..2f51c2c3
--- /dev/null
+++ b/common/src/main/java/pl/project13/maven/git/GitDataLoader.java
@@ -0,0 +1,339 @@
+/*
+ * This file is part of git-commit-id-plugin by Konrad Malawski
+ *
+ * git-commit-id-plugin is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * git-commit-id-plugin is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with git-commit-id-plugin. If not, see .
+ */
+
+package pl.project13.maven.git;
+
+import java.io.File;
+import java.io.IOException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Map;
+import java.util.Properties;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
+import org.eclipse.jgit.api.errors.GitAPIException;
+import org.eclipse.jgit.lib.AbbreviatedObjectId;
+import org.eclipse.jgit.lib.Constants;
+import org.eclipse.jgit.lib.ObjectReader;
+import org.eclipse.jgit.lib.Ref;
+import org.eclipse.jgit.lib.Repository;
+import org.eclipse.jgit.revwalk.RevCommit;
+import org.eclipse.jgit.revwalk.RevWalk;
+import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
+
+import pl.project13.jgit.DescribeCommand;
+import pl.project13.jgit.DescribeResult;
+import pl.project13.maven.git.log.LoggerBridge;
+import pl.project13.maven.git.log.StdOutLoggerBridge;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Strings;
+
+/**
+ * Extracted from the Mojo to reuse code.
+ *
+ */
+public class GitDataLoader {
+
+ // these properties will be exposed to maven
+ public static final String BRANCH = "branch";
+ public static final String COMMIT_ID = "commit.id";
+ public static final String COMMIT_ID_ABBREV = "commit.id.abbrev";
+ public static final String COMMIT_DESCRIBE = "commit.id.describe";
+ public static final String BUILD_AUTHOR_NAME = "build.user.name";
+ public static final String BUILD_AUTHOR_EMAIL = "build.user.email";
+ public static final String BUILD_TIME = "build.time";
+ public static final String COMMIT_AUTHOR_NAME = "commit.user.name";
+ public static final String COMMIT_AUTHOR_EMAIL = "commit.user.email";
+ public static final String COMMIT_MESSAGE_FULL = "commit.message.full";
+ public static final String COMMIT_MESSAGE_SHORT = "commit.message.short";
+ public static final String COMMIT_TIME = "commit.time";
+ public static final String REMOTE_ORIGIN_URL = "remote.origin.url";
+
+ private File rootDirectory;
+ private int abbrevLength = 7;
+ private String prefixDot = "git";
+ private String dateFormat = "dd.MM.yyyy '@' HH:mm:ss z";
+ private GitDescribeConfig gitDescribe;
+ private LoggerBridge loggerBridge;
+ private boolean verbose = false;
+
+ private GitDataLoader(GitDataLoaderBuilder builder){
+ this.rootDirectory = builder.workingTreeDirectory;
+ this.abbrevLength = builder.abbrevLength;
+ this.prefixDot = builder.prefixDot;
+ this.dateFormat = builder.dateFormat;
+
+ this.gitDescribe = builder.gitDescribe;
+ this.loggerBridge = builder.loggerBridge;
+ this.verbose = builder.verbose;
+ }
+
+ public void loadGitData(Properties properties) throws IOException, GitDataLoaderException {
+ Repository git = getGitRepository();
+ ObjectReader objectReader = git.newObjectReader();
+
+ // git.user.name
+ String userName = git.getConfig().getString("user", null, "name");
+ put(properties, BUILD_AUTHOR_NAME, userName);
+
+ // git.user.email
+ String userEmail = git.getConfig().getString("user", null, "email");
+ put(properties, BUILD_AUTHOR_EMAIL, userEmail);
+
+ // more details parsed out bellow
+ Ref HEAD = git.getRef(Constants.HEAD);
+ if (HEAD == null) {
+ throw new GitDataLoaderException("Could not get HEAD Ref, are you sure you've set the dotGitDirectory property of this plugin to a valid path?");
+ }
+ RevWalk revWalk = new RevWalk(git);
+ RevCommit headCommit = revWalk.parseCommit(HEAD.getObjectId());
+ revWalk.markStart(headCommit);
+
+ try {
+ // git.branch
+ String branch = determineBranchName(git, System.getenv());
+ put(properties, BRANCH, branch);
+
+ // git.commit.id.describe
+// maybePutGitDescribe(properties, git);
+
+ // git.commit.id
+ put(properties, COMMIT_ID, headCommit.getName());
+
+ // git.commit.id.abbrev
+ putAbbrevCommitId(objectReader, properties, headCommit, abbrevLength);
+
+ // git.commit.author.name
+ String commitAuthor = headCommit.getAuthorIdent().getName();
+ put(properties, COMMIT_AUTHOR_NAME, commitAuthor);
+
+ // git.commit.author.email
+ String commitEmail = headCommit.getAuthorIdent().getEmailAddress();
+ put(properties, COMMIT_AUTHOR_EMAIL, commitEmail);
+
+ // git commit.message.full
+ String fullMessage = headCommit.getFullMessage();
+ put(properties, COMMIT_MESSAGE_FULL, fullMessage);
+
+ // git commit.message.short
+ String shortMessage = headCommit.getShortMessage();
+ put(properties, COMMIT_MESSAGE_SHORT, shortMessage);
+
+ long timeSinceEpoch = headCommit.getCommitTime();
+ Date commitDate = new Date(timeSinceEpoch * 1000); // git is "by sec" and java is "by ms"
+ SimpleDateFormat smf = new SimpleDateFormat(dateFormat);
+ put(properties, COMMIT_TIME, smf.format(commitDate));
+
+ // git remote.origin.url
+ String remoteOriginUrl = git.getConfig().getString("remote", "origin", "url");
+ put(properties, REMOTE_ORIGIN_URL, remoteOriginUrl);
+
+ //
+ loadBuildTimeData(properties);
+ } finally {
+ revWalk.dispose();
+ }
+ }
+
+ @Nonnull
+ private Repository getGitRepository() throws GitDataLoaderException {
+ Repository repository;
+
+ FileRepositoryBuilder repositoryBuilder = new FileRepositoryBuilder();
+ try {
+ repository = repositoryBuilder.setWorkTree(rootDirectory).setMustExist(true)
+// .setGitDir(rootDirectory)
+ .readEnvironment() // scan environment GIT_* variables
+ .findGitDir() // scan up the file system tree
+ .build();
+ } catch (IOException e) {
+ throw new GitDataLoaderException("Could not initialize repository...", e);
+ }
+
+ if (repository == null) {
+ throw new GitDataLoaderException("Could not create git repository. Are you sure '" + rootDirectory.getAbsolutePath() + "' is the valid Git root for your project?");
+ }
+
+ return repository;
+ }
+
+ private void put(@Nonnull Properties properties, String key, String value) {
+ putWithoutPrefix(properties, prefixDot + key, value);
+ }
+
+ private void putWithoutPrefix(@Nonnull Properties properties, String key, String value) {
+ if (!isNotEmpty(value)) {
+ value = "Unknown";
+ }
+
+ log(key, value);
+ properties.put(key, value);
+ }
+
+ private boolean isNotEmpty(@Nullable String value) {
+ return null != value && !" ".equals(value.trim().replaceAll(" ", ""));
+ }
+
+ private void putAbbrevCommitId(ObjectReader objectReader, Properties properties, RevCommit headCommit, int abbrevLength) throws GitDataLoaderException {
+ if (abbrevLength < 2 || abbrevLength > 40) {
+ throw new GitDataLoaderException("Abbreviated commit id lenght must be between 2 and 40, inclusive! Was [%s]. ".codePointBefore(abbrevLength) +
+ "Please fix your configuration (the element).");
+ }
+
+ try {
+ AbbreviatedObjectId abbreviatedObjectId = objectReader.abbreviate(headCommit, abbrevLength);
+ put(properties, COMMIT_ID_ABBREV, abbreviatedObjectId.name());
+ } catch (IOException e) {
+ throw new GitDataLoaderException("Unable to abbreviate commit id! " +
+ "You may want to investigate the element in your configuration.", e);
+ }
+ }
+
+ /**
+ * If running within Jenkins/Hudosn, honor the branch name passed via GIT_BRANCH env var. This
+ * is necessary because Jenkins/Hudson alwways invoke build in a detached head state.
+ *
+ * @param git
+ * @param env
+ * @return results of git.getBranch() or, if in Jenkins/Hudson, value of GIT_BRANCH
+ */
+ protected String determineBranchName(Repository git, Map env) throws IOException {
+ if (runningOnBuildServer(env)) {
+ return determineBranchNameOnBuildServer(git, env);
+ } else {
+ return git.getBranch();
+ }
+ }
+
+ /**
+ * Is "Jenkins aware", and prefers {@code GIT_BRANCH} to getting the branch via git if that enviroment variable is set.
+ * The {@GIT_BRANCH} variable is set by Jenkins/Hudson when put in detached HEAD state, but it still knows which branch was cloned.
+ */
+ protected String determineBranchNameOnBuildServer(Repository git, Map env) throws IOException {
+ String enviromentBasedBranch = env.get("GIT_BRANCH");
+ if(Strings.isNullOrEmpty(enviromentBasedBranch)) {
+ log("Detected that running on CI enviroment, but using repository branch, no GIT_BRANCH detected.");
+ return git.getBranch();
+ }else {
+ log("Using environment variable based branch name.", "GIT_BRANCH =", enviromentBasedBranch);
+ return enviromentBasedBranch;
+ }
+ }
+
+ /**
+ * Detects if we're running on Jenkins or Hudson, based on expected env variables.
+ *
+ * TODO: How can we detect Bamboo, TeamCity etc? Pull requests welcome.
+ *
+ * @return true if running
+ * @see JenkinsSetEnvironmentVariables
+ * @param env
+ */
+ private boolean runningOnBuildServer(Map env) {
+ return env.containsKey("HUDSON_URL") || env.containsKey("JENKINS_URL");
+ }
+
+ void maybePutGitDescribe(@Nonnull Properties properties,
+ @Nonnull Repository repository) throws GitDataLoaderException {
+ if (gitDescribe == null || !gitDescribe.isSkip()) {
+ putGitDescribe(properties, repository);
+ }
+ }
+
+ @VisibleForTesting
+ void putGitDescribe(@Nonnull Properties properties, @Nonnull Repository repository) throws GitDataLoaderException {
+ try {
+ DescribeResult describeResult = DescribeCommand
+ .on(repository)
+ .withLoggerBridge(loggerBridge)
+ .setVerbose(verbose)
+ .apply(gitDescribe)
+ .call();
+
+ put(properties, COMMIT_DESCRIBE, describeResult.toString());
+ } catch (GitAPIException ex) {
+ throw new GitDataLoaderException("Unable to obtain git.commit.id.describe information", ex);
+ }
+ }
+
+ void loadBuildTimeData(@Nonnull Properties properties) {
+ Date commitDate = new Date();
+ SimpleDateFormat smf = new SimpleDateFormat(dateFormat);
+ put(properties, BUILD_TIME, smf.format(commitDate));
+ }
+
+ private void log(Object... parts){
+ loggerBridge.log(parts);
+ }
+
+ public static class GitDataLoaderBuilder {
+
+ private File workingTreeDirectory;
+ private int abbrevLength = 7;
+ private String prefixDot = "git";
+ private String dateFormat = "dd.MM.yyyy '@' HH:mm:ss z";
+
+ private GitDescribeConfig gitDescribe;
+ private LoggerBridge loggerBridge = new StdOutLoggerBridge(false);
+ private boolean verbose = false;
+
+
+ public GitDataLoaderBuilder withWorkingTreeDirectory(File workingTreeDirectory){
+ // TODO, validate here
+ this.workingTreeDirectory = workingTreeDirectory;
+ return this;
+ }
+
+ public GitDataLoaderBuilder withAbbrevationLength(int abbrevationLength){
+ this.abbrevLength = abbrevationLength;
+ return this;
+ }
+
+ public GitDataLoaderBuilder withPrefixDot(String prefixDot){
+ this.prefixDot = prefixDot;
+ return this;
+ }
+
+ public GitDataLoaderBuilder withDateFormat(String dateformat){
+ this.dateFormat = dateformat;
+ return this;
+ }
+
+ public GitDataLoaderBuilder withGitDescribe(GitDescribeConfig gitDescribeConfig){
+ this.gitDescribe = gitDescribeConfig;
+ return this;
+ }
+
+ public GitDataLoaderBuilder withLoggerBridge(LoggerBridge loggerBridge){
+ this.loggerBridge = loggerBridge;
+ return this;
+ }
+
+ public GitDataLoaderBuilder verbose(boolean verbose){
+ this.verbose = verbose;
+ return this;
+ }
+
+ public GitDataLoader build(){
+ return new GitDataLoader(this);
+ }
+
+ }
+}
diff --git a/common/src/main/java/pl/project13/maven/git/GitDataLoaderException.java b/common/src/main/java/pl/project13/maven/git/GitDataLoaderException.java
new file mode 100644
index 00000000..26c80e65
--- /dev/null
+++ b/common/src/main/java/pl/project13/maven/git/GitDataLoaderException.java
@@ -0,0 +1,39 @@
+/*
+ * This file is part of git-commit-id-plugin by Konrad Malawski
+ *
+ * git-commit-id-plugin is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * git-commit-id-plugin is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with git-commit-id-plugin. If not, see .
+ */
+
+package pl.project13.maven.git;
+
+/**
+ * To wrap exceptions when informations from the repository are loaded.
+ *
+ */
+public class GitDataLoaderException extends Exception {
+
+ private static final long serialVersionUID = 1L;
+
+ public GitDataLoaderException(String message){
+ super(message);
+ }
+
+ public GitDataLoaderException(String message, Throwable cause){
+ super(message, cause);
+ }
+
+ public GitDataLoaderException(Throwable cause){
+ super(cause);
+ }
+}
diff --git a/src/main/java/pl/project13/maven/git/GitDescribeConfig.java b/common/src/main/java/pl/project13/maven/git/GitDescribeConfig.java
similarity index 96%
rename from src/main/java/pl/project13/maven/git/GitDescribeConfig.java
rename to common/src/main/java/pl/project13/maven/git/GitDescribeConfig.java
index e3d049ba..cdd4bfbd 100644
--- a/src/main/java/pl/project13/maven/git/GitDescribeConfig.java
+++ b/common/src/main/java/pl/project13/maven/git/GitDescribeConfig.java
@@ -21,7 +21,6 @@
* Represents options passed in via maven configuration,
* corresponds to options of git-describe.
*/
-@SuppressWarnings("JavaDoc")
public class GitDescribeConfig {
/**
@@ -32,7 +31,7 @@ public class GitDescribeConfig {
*
* @parameter default-value=false
*/
- private boolean skip;
+ private boolean skip = false;
/**
* --always
@@ -42,7 +41,7 @@ public class GitDescribeConfig {
*
* @parameter default-value=true
*/
- private boolean always;
+ private boolean always = true;
/**
* --dirty[=mark]
@@ -53,7 +52,7 @@ public class GitDescribeConfig {
*
* @parameter default-value="devel"
*/
- private String dirty;
+ private String dirty = "devel";
/**
*--match glob-pattern
@@ -63,7 +62,7 @@ public class GitDescribeConfig {
*
* @parameter default-value="*"
*/
- private String match;
+ private String match = "*";
/**
* --abbrev=N
@@ -103,7 +102,7 @@ public class GitDescribeConfig {
*
* @parameter default-value=7
*/
- private int abbrev;
+ private int abbrev = 7;
/**
* --tags
@@ -136,7 +135,7 @@ public class GitDescribeConfig {
*
* @parameter
*/
- private Boolean tags;
+ private Boolean tags = Boolean.FALSE;
/**
* --long
@@ -149,7 +148,7 @@ public class GitDescribeConfig {
*
* false
by default.
*/
- private Boolean forceLongFormat;
+ private Boolean forceLongFormat = Boolean.FALSE;
public GitDescribeConfig() {
}
diff --git a/src/main/java/pl/project13/maven/git/GitRepositoryState.java b/common/src/main/java/pl/project13/maven/git/GitRepositoryState.java
similarity index 97%
rename from src/main/java/pl/project13/maven/git/GitRepositoryState.java
rename to common/src/main/java/pl/project13/maven/git/GitRepositoryState.java
index e7b6d676..2f671aa7 100644
--- a/src/main/java/pl/project13/maven/git/GitRepositoryState.java
+++ b/common/src/main/java/pl/project13/maven/git/GitRepositoryState.java
@@ -19,7 +19,7 @@
//import org.codehaus.jackson.annotate.JsonWriteNullProperties;
-import org.jetbrains.annotations.NotNull;
+import javax.annotation.Nonnull;
/**
* A spring controlled bean that will be injected
@@ -170,7 +170,7 @@ public String toJson() {
return sb.append("}").toString();
}
- private void appendProperty(@NotNull StringBuilder sb, String label, String value) {
+ private void appendProperty(@Nonnull StringBuilder sb, String label, String value) {
sb.append(String.format("\"%s\": \"%s\",", label, value));
}
diff --git a/common/src/main/java/pl/project13/maven/git/PropertiesExcludeFilter.java b/common/src/main/java/pl/project13/maven/git/PropertiesExcludeFilter.java
new file mode 100644
index 00000000..d83cdb8e
--- /dev/null
+++ b/common/src/main/java/pl/project13/maven/git/PropertiesExcludeFilter.java
@@ -0,0 +1,59 @@
+/*
+ * This file is part of git-commit-id-plugin by Konrad Malawski
+ *
+ * git-commit-id-plugin is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * git-commit-id-plugin is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with git-commit-id-plugin. If not, see .
+ */
+
+package pl.project13.maven.git;
+
+import java.util.List;
+import java.util.Properties;
+
+import javax.annotation.Nullable;
+
+import com.google.common.base.Function;
+import com.google.common.base.Predicate;
+import com.google.common.base.Predicates;
+import com.google.common.collect.Lists;
+
+/**
+ * Exclude properties configured for exclusion.
+ *
+ */
+public class PropertiesExcludeFilter {
+
+ public static void filterNot(Properties properties, @Nullable List exclusions) {
+ if (exclusions == null)
+ return;
+
+ List> excludePredicates = Lists.transform(exclusions, new Function>() {
+ @Override
+ public Predicate apply(String exclude) {
+ return Predicates.containsPattern(exclude);
+ }
+ });
+
+ Predicate shouldExclude = Predicates.alwaysFalse();
+ for (Predicate predicate : excludePredicates) {
+ shouldExclude = Predicates.or(shouldExclude, predicate);
+ }
+
+ for (String key : properties.stringPropertyNames()) {
+ if (shouldExclude.apply(key)) {
+ System.out.println("shouldExclude.apply(" + key +") = " + shouldExclude.apply(key));
+ properties.remove(key);
+ }
+ }
+ }
+}
diff --git a/src/main/java/pl/project13/maven/git/log/LoggerBridge.java b/common/src/main/java/pl/project13/maven/git/log/LoggerBridge.java
similarity index 100%
rename from src/main/java/pl/project13/maven/git/log/LoggerBridge.java
rename to common/src/main/java/pl/project13/maven/git/log/LoggerBridge.java
diff --git a/src/main/java/pl/project13/maven/git/log/StdOutLoggerBridge.java b/common/src/main/java/pl/project13/maven/git/log/StdOutLoggerBridge.java
similarity index 100%
rename from src/main/java/pl/project13/maven/git/log/StdOutLoggerBridge.java
rename to common/src/main/java/pl/project13/maven/git/log/StdOutLoggerBridge.java
diff --git a/src/main/java/pl/project13/maven/git/util/Pair.java b/common/src/main/java/pl/project13/maven/git/util/Pair.java
similarity index 93%
rename from src/main/java/pl/project13/maven/git/util/Pair.java
rename to common/src/main/java/pl/project13/maven/git/util/Pair.java
index fe0cbf18..763aa893 100644
--- a/src/main/java/pl/project13/maven/git/util/Pair.java
+++ b/common/src/main/java/pl/project13/maven/git/util/Pair.java
@@ -17,16 +17,17 @@
package pl.project13.maven.git.util;
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
import com.google.common.base.Preconditions;
-import org.jetbrains.annotations.NotNull;
-import org.jetbrains.annotations.Nullable;
public class Pair {
- @NotNull
+ @Nonnull
public final A first;
- @NotNull
+ @Nonnull
public final B second;
@SuppressWarnings("ConstantConditions")
@@ -38,7 +39,7 @@ public Pair(A first, B second) {
this.second = second;
}
- @NotNull
+ @Nonnull
public static Pair of(A first, B second) {
return new Pair(first, second);
}
@@ -72,7 +73,7 @@ public int hashCode() {
return result;
}
- @NotNull
+ @Nonnull
@Override
public String toString() {
return String.format("Pair(%s, %s)", first, second);
diff --git a/common/src/test/java/keepgit b/common/src/test/java/keepgit
new file mode 100644
index 00000000..e69de29b
diff --git a/gradle/pom.xml b/gradle/pom.xml
new file mode 100644
index 00000000..fa0eb29c
--- /dev/null
+++ b/gradle/pom.xml
@@ -0,0 +1,129 @@
+
+ 4.0.0
+
+
+ pl.project13.maven
+ git-commit-id-plugin-parent
+ 2.1.10-SNAPSHOT
+
+
+ pl.project13.maven
+ git-commit-id-plugin-gradle
+ Git Commit Id Plugin Maven Gradle
+
+
+ 1.6
+ 2.2.1
+
+
+
+
+ ${project.groupId}
+ git-commit-id-plugin-common
+ ${project.version}
+
+
+ org.codehaus.groovy
+ groovy
+ ${groovy.version}
+ provided
+
+
+ org.gradle
+ gradle-core
+ ${gradle.version}
+ provided
+
+
+ org.gradle
+ gradle-base-services
+ ${gradle.version}
+ provided
+
+
+ org.gradle
+ gradle-base-services-groovy
+ ${gradle.version}
+ provided
+
+
+ org.gradle
+ gradle-plugins
+ ${gradle.version}
+ provided
+
+
+
+
+ src/main/groovy
+
+
+ maven-compiler-plugin
+ 3.1
+
+ groovy-eclipse-compiler
+
+
+
+ org.codehaus.groovy
+ groovy-eclipse-compiler
+ 2.8.0-01
+
+
+ org.codehaus.groovy
+ groovy-eclipse-batch
+ 2.1.5-03
+
+
+
+
+
+
+
+
+ org.eclipse.m2e
+ lifecycle-mapping
+ 1.0.0
+
+
+
+
+
+
+ org.apache.maven.plugins
+
+
+ maven-compiler-plugin
+
+
+ [3.1,)
+
+
+ compile
+ testCompile
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ gradle
+ http://repo.gradle.org/gradle/libs-releases-local
+
+ true
+
+
+ false
+
+
+
+
\ No newline at end of file
diff --git a/gradle/src/main/groovy/pl/project13/gradle/git/GitCommitIdPlugin.groovy b/gradle/src/main/groovy/pl/project13/gradle/git/GitCommitIdPlugin.groovy
new file mode 100644
index 00000000..825778c3
--- /dev/null
+++ b/gradle/src/main/groovy/pl/project13/gradle/git/GitCommitIdPlugin.groovy
@@ -0,0 +1,68 @@
+/*
+ * This file is part of git-commit-id-plugin by Konrad Malawski
+ *
+ * git-commit-id-plugin is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * git-commit-id-plugin is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with git-commit-id-plugin. If not, see .
+ */
+
+package pl.project13.gradle.git
+
+import org.gradle.api.GradleException
+import org.gradle.api.Plugin
+import org.gradle.api.Project
+import org.gradle.api.logging.Logger
+
+import pl.project13.maven.git.GitDataLoader
+import pl.project13.maven.git.GitDataLoaderException
+import pl.project13.maven.git.GitDescribeConfig
+import pl.project13.maven.git.GitDataLoader.GitDataLoaderBuilder
+
+
+/**
+ * Simple, but hopefully works.
+ *
+ */
+class GitCommitIdPlugin implements Plugin {
+
+ private File rootDirectory;
+ private Logger logger;
+
+ @Override
+ public void apply(Project project) {
+ rootDirectory = project.getRootDir();
+ logger = project.getLogger();
+
+ String prefix = "git"
+ String prefixDot = prefix + ".";
+
+ Properties properties = new Properties();
+
+ GitDataLoader gitDataLoader = new GitDataLoaderBuilder()
+ .withWorkingTreeDirectory(rootDirectory)
+// .withAbbrevationLength(abbrevLength)
+// .withDateFormat(dateFormat)
+ .withGitDescribe(new GitDescribeConfig())
+ .withPrefixDot(prefixDot)
+// .withLoggerBridge(loggerBridge)
+ .build();
+
+ try {
+ gitDataLoader.loadGitData(properties);
+ } catch (GitDataLoaderException e) {
+ throw new GradleException(e.getMessage(), e);
+ }
+
+ System.getProperties().putAll(properties)
+ }
+
+}
diff --git a/gradle/src/main/resources/META-INF/gradle-plugins/git-commit-id.properties b/gradle/src/main/resources/META-INF/gradle-plugins/git-commit-id.properties
new file mode 100644
index 00000000..2f17ddac
--- /dev/null
+++ b/gradle/src/main/resources/META-INF/gradle-plugins/git-commit-id.properties
@@ -0,0 +1,17 @@
+#
+# This file is part of git-commit-id-plugin by Konrad Malawski
+#
+# git-commit-id-plugin is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# git-commit-id-plugin is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with git-commit-id-plugin. If not, see .
+#
+implementation-class=pl.project13.gradle.git.GitCommitIdPlugin
\ No newline at end of file
diff --git a/gradle/src/test/groovy/keepgit b/gradle/src/test/groovy/keepgit
new file mode 100644
index 00000000..e69de29b
diff --git a/maven/keepgit b/maven/keepgit
new file mode 100644
index 00000000..e69de29b
diff --git a/maven/pom.xml b/maven/pom.xml
new file mode 100644
index 00000000..9b4d6654
--- /dev/null
+++ b/maven/pom.xml
@@ -0,0 +1,295 @@
+
+ 4.0.0
+
+
+ pl.project13.maven
+ git-commit-id-plugin-parent
+ 2.1.10-SNAPSHOT
+
+
+ pl.project13.maven
+ git-commit-id-plugin
+ maven-plugin
+
+ Git Commit Id Plugin Maven Mojo
+
+ git-commit-id-plugin is a plugin quite similar to
+ https://fisheye.codehaus.org/browse/mojo/tags/buildnumber-maven-plugin-1.0-beta-4 for example but as buildnumber
+ only supports svn (which is very sad) and cvs (which is even more sad).
+ This plugin makes basic repository information available through maven resources. This can be used to display
+ "what version is this?" or "who has deployed this and when, from which branch?" information at runtime - making
+ it easy to find things like "oh, that isn't deployed yet, I'll test it tomorrow" and making both testers and
+ developers life easier.
+
+ The data currently exported is like this (that's the end effect from the GitRepositoryState Bean):
+ {
+ "branch" : "testing-maven-git-plugin",
+ "commitTime" : "06.01.1970 @ 16:16:26 CET",
+ "commitId" : "787e39f61f99110e74deed68ab9093088d64b969",
+ "commitUserName" : "Konrad Malawski",
+ "commitUserEmail" : "konrad.malawski@java.pl",
+ "commitMessageFull" : "releasing my fun plugin :-) + fixed some typos + cleaned up directory structure + added
+ license etc",
+ "commitMessageShort" : "releasing my fun plugin :-)",
+ "buildTime" : "06.01.1970 @ 16:17:53 CET",
+ "buildUserName" : "Konrad Malawski",
+ "buildUserEmail" : "konrad.malawski@java.pl"
+ }
+
+ Note that the data is exported via maven resource filtering and is really easy to use with spring -
+ which I've explained in detail in this readme https://github.com/ktoso/maven-git-commit-id-plugin
+
+ http://www.blog.project13.pl
+
+
+
+ ktoso
+ Konrad Malawski
+ konrad.malawski@java.pl
+ Project13.pl
+ http://blog.project13.pl
+
+
+
+
+ [${maven-plugin-api.version},)
+
+
+
+
+ GNU Lesser General Public License 3.0
+ http://www.gnu.org/licenses/lgpl-3.0.txt
+
+
+
+
+ git@github.com/ktoso/maven-git-commit-id-plugin.git
+ scm:git:https://github.com/ktoso/maven-git-commit-id-plugin
+ scm:git:git@github.com:ktoso/maven-git-commit-id-plugin.git
+
+
+
+ 2.2.1
+
+ 2.0.0.201206130900-r
+ 4.10
+ 1.9.0
+
+ 1.6
+ UTF-8
+ UTF-8
+ 1.4
+
+
+
+
+ ${project.groupId}
+ ${project.artifactId}-common
+ ${project.version}
+
+
+
+
+ org.apache.maven
+ maven-plugin-api
+ ${maven-plugin-api.version}
+
+
+ org.apache.maven
+ maven-project
+ ${maven-plugin-api.version}
+
+
+
+ com.fasterxml.jackson.core
+ jackson-databind
+ 2.2.3
+
+
+
+ com.google.inject
+ guice
+ 2.0
+
+
+
+
+ joda-time
+ joda-time
+ 2.0
+
+
+
+
+ com.google.guava
+ guava
+ 15.0
+
+
+
+
+
+
+ com.google.code.findbugs
+ jsr305
+
+
+
+
+
+ org.eclipse.jgit
+ org.eclipse.jgit
+ ${jgit.version}
+
+
+
+
+ junit
+ junit
+ ${junit.version}
+ test
+
+
+
+ org.easytesting
+ fest-assert
+ ${fest-assert.version}
+ test
+
+
+
+ org.codehaus.plexus
+ plexus-utils
+ 2.0.5
+ test
+
+
+
+ org.mockito
+ mockito-all
+ ${mockito.version}
+ test
+
+
+
+ org.apache.commons
+ commons-io
+ 1.3.2
+ jar
+ test
+
+
+
+
+
+
+
+ src/main/resources
+ true
+
+ **/*.properties
+ **/*.xml
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+ 2.5.1
+
+ UTF-8
+ ${java.target}
+ ${java.target}
+
+
+
+
+ maven-plugin-plugin
+ 3.2
+
+
+ generated-helpmojo
+
+ helpmojo
+
+
+ git-commit-id
+ pl.project13.maven
+
+
+
+
+
+
+
+
+
+ gpg
+
+
+
+
+ org.apache.maven.plugins
+ maven-gpg-plugin
+
+
+ sign-artifacts
+ verify
+
+ sign
+
+
+
+
+
+
+
+
+
+
diff --git a/src/main/java/pl/project13/maven/git/GitCommitIdMojo.java b/maven/src/main/java/pl/project13/maven/git/GitCommitIdMojo.java
similarity index 54%
rename from src/main/java/pl/project13/maven/git/GitCommitIdMojo.java
rename to maven/src/main/java/pl/project13/maven/git/GitCommitIdMojo.java
index 8f88366c..a31eba8c 100644
--- a/src/main/java/pl/project13/maven/git/GitCommitIdMojo.java
+++ b/maven/src/main/java/pl/project13/maven/git/GitCommitIdMojo.java
@@ -17,36 +17,27 @@
package pl.project13.maven.git;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Function;
-import com.google.common.base.Predicate;
-import com.google.common.base.Predicates;
-import com.google.common.collect.Lists;
-import com.google.common.io.Closeables;
-import com.google.common.io.Files;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.Collections;
+import java.util.List;
+import java.util.Properties;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
-import org.eclipse.jgit.api.errors.GitAPIException;
-import org.eclipse.jgit.lib.*;
-import org.eclipse.jgit.revwalk.RevCommit;
-import org.eclipse.jgit.revwalk.RevWalk;
-import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
-import org.jetbrains.annotations.NotNull;
-import org.jetbrains.annotations.Nullable;
-import pl.project13.jgit.DescribeCommand;
-import pl.project13.jgit.DescribeResult;
+
+import pl.project13.maven.git.GitDataLoader.GitDataLoaderBuilder;
import pl.project13.maven.git.log.LoggerBridge;
import pl.project13.maven.git.log.MavenLoggerBridge;
-import java.io.File;
-import java.io.FileWriter;
-import java.io.IOException;
-import java.text.SimpleDateFormat;
-import java.util.*;
-
-import static com.google.common.base.Strings.isNullOrEmpty;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.io.Closeables;
+import com.google.common.io.Files;
/**
* Goal which puts git build-time information into property files or maven's properties.
@@ -58,23 +49,22 @@
* @threadSafe true
* @since 1.0
*/
-@SuppressWarnings({"JavaDoc"})
public class GitCommitIdMojo extends AbstractMojo {
- // these properties will be exposed to maven
- public static final String BRANCH = "branch";
- public static final String COMMIT_ID = "commit.id";
- public static final String COMMIT_ID_ABBREV = "commit.id.abbrev";
- public static final String COMMIT_DESCRIBE = "commit.id.describe";
- public static final String BUILD_AUTHOR_NAME = "build.user.name";
- public static final String BUILD_AUTHOR_EMAIL = "build.user.email";
+// // these properties will be exposed to maven
+// public static final String BRANCH = "branch";
+// public static final String COMMIT_ID = "commit.id";
+// public static final String COMMIT_ID_ABBREV = "commit.id.abbrev";
+// public static final String COMMIT_DESCRIBE = "commit.id.describe";
+// public static final String BUILD_AUTHOR_NAME = "build.user.name";
+// public static final String BUILD_AUTHOR_EMAIL = "build.user.email";
public static final String BUILD_TIME = "build.time";
- public static final String COMMIT_AUTHOR_NAME = "commit.user.name";
- public static final String COMMIT_AUTHOR_EMAIL = "commit.user.email";
- public static final String COMMIT_MESSAGE_FULL = "commit.message.full";
- public static final String COMMIT_MESSAGE_SHORT = "commit.message.short";
- public static final String COMMIT_TIME = "commit.time";
- public static final String REMOTE_ORIGIN_URL = "remote.origin.url";
+// public static final String COMMIT_AUTHOR_NAME = "commit.user.name";
+// public static final String COMMIT_AUTHOR_EMAIL = "commit.user.email";
+// public static final String COMMIT_MESSAGE_FULL = "commit.message.full";
+// public static final String COMMIT_MESSAGE_SHORT = "commit.message.short";
+// public static final String COMMIT_TIME = "commit.time";
+// public static final String REMOTE_ORIGIN_URL = "remote.origin.url";
/**
* The maven project.
@@ -82,7 +72,6 @@ public class GitCommitIdMojo extends AbstractMojo {
* @parameter property="project"
* @readonly
*/
- @SuppressWarnings("UnusedDeclaration")
MavenProject project;
/**
@@ -91,7 +80,6 @@ public class GitCommitIdMojo extends AbstractMojo {
* @parameter property="reactorProjects"
* @readonly
*/
- @SuppressWarnings("UnusedDeclaration")
private List reactorProjects;
/**
@@ -103,7 +91,6 @@ public class GitCommitIdMojo extends AbstractMojo {
*
* @parameter default-value="true"
*/
- @SuppressWarnings("UnusedDeclaration")
private boolean injectAllReactorProjects;
/**
@@ -113,7 +100,6 @@ public class GitCommitIdMojo extends AbstractMojo {
*
* @parameter default-value="false"
*/
- @SuppressWarnings("UnusedDeclaration")
private boolean verbose;
/**
@@ -122,7 +108,6 @@ public class GitCommitIdMojo extends AbstractMojo {
*
* @parameter parameter="git.skipPoms" default-value="true"
*/
- @SuppressWarnings("UnusedDeclaration")
private boolean skipPoms;
/**
@@ -134,7 +119,6 @@ public class GitCommitIdMojo extends AbstractMojo {
*
* @parameter default-value="false"
*/
- @SuppressWarnings("UnusedDeclaration")
private boolean generateGitPropertiesFile;
/**
@@ -146,7 +130,6 @@ public class GitCommitIdMojo extends AbstractMojo {
*
* @parameter default-value="${project.build.outputDirectory}/git.properties"
*/
- @SuppressWarnings("UnusedDeclaration")
private String generateGitPropertiesFilename;
/**
@@ -154,7 +137,6 @@ public class GitCommitIdMojo extends AbstractMojo {
*
* @parameter default-value="${project.basedir}/.git"
*/
- @SuppressWarnings("UnusedDeclaration")
private File dotGitDirectory;
/**
@@ -165,7 +147,6 @@ public class GitCommitIdMojo extends AbstractMojo {
*
* @parameter
*/
- @SuppressWarnings("UnusedDeclaration")
private GitDescribeConfig gitDescribe;
/**
@@ -187,7 +168,6 @@ public class GitCommitIdMojo extends AbstractMojo {
*
* @parameter default-value=7
*/
- @SuppressWarnings("UnusedDeclaration")
private int abbrevLength;
/**
@@ -195,7 +175,6 @@ public class GitCommitIdMojo extends AbstractMojo {
*
* @parameter default-value="properties"
*/
- @SuppressWarnings("UnusedDeclaration")
private String format;
/**
@@ -203,7 +182,6 @@ public class GitCommitIdMojo extends AbstractMojo {
*
* @parameter default-value="git"
*/
- @SuppressWarnings("UnusedDeclaration")
private String prefix;
private String prefixDot;
@@ -213,7 +191,6 @@ public class GitCommitIdMojo extends AbstractMojo {
*
* @parameter default-value="dd.MM.yyyy '@' HH:mm:ss z"
*/
- @SuppressWarnings("UnusedDeclaration")
private String dateFormat;
/**
@@ -222,7 +199,6 @@ public class GitCommitIdMojo extends AbstractMojo {
*
* @parameter default-value="true"
*/
- @SuppressWarnings("UnusedDeclaration")
private boolean failOnNoGitDirectory;
/**
@@ -237,7 +213,6 @@ public class GitCommitIdMojo extends AbstractMojo {
*
* @parameter default-value="true"
*/
- @SuppressWarnings("UnusedDeclaration")
private boolean failOnUnableToExtractRepoInfo;
/**
@@ -246,7 +221,6 @@ public class GitCommitIdMojo extends AbstractMojo {
* @parameter default-value="false"
* @since 2.1.8
*/
- @SuppressWarnings("UnusedDeclaration")
private boolean skip = false;
/**
@@ -262,7 +236,6 @@ public class GitCommitIdMojo extends AbstractMojo {
* @parameter
* @since 2.1.9
*/
- @SuppressWarnings("UnusedDeclaration")
private List excludeProperties = Collections.emptyList();
@@ -273,7 +246,7 @@ public class GitCommitIdMojo extends AbstractMojo {
boolean runningTests = false;
- @NotNull
+ @Nonnull
LoggerBridge loggerBridge = new MavenLoggerBridge(getLog(), true);
public void execute() throws MojoExecutionException {
@@ -304,9 +277,29 @@ public void execute() throws MojoExecutionException {
properties = initProperties();
prefixDot = prefix + ".";
- loadGitData(properties);
- filterNot(properties, excludeProperties);
- loadBuildTimeData(properties);
+// loadGitData(properties);
+
+ GitDataLoader gitDataLoader = new GitDataLoaderBuilder()
+ .withWorkingTreeDirectory(dotGitDirectory.getParentFile())
+ .withAbbrevationLength(abbrevLength)
+ .withDateFormat(dateFormat)
+ .withGitDescribe(gitDescribe)
+ .withPrefixDot(prefixDot)
+ .withLoggerBridge(loggerBridge)
+ .build();
+
+ try {
+ gitDataLoader.loadGitData(properties);
+ } catch (GitDataLoaderException e) {
+ throw new MojoExecutionException(e.getMessage(), e);
+ }
+
+// filterNot(properties, excludeProperties);
+ PropertiesExcludeFilter.filterNot(properties, excludeProperties);
+
+ // now in GitDataLoader
+// loadBuildTimeData(properties);
+
logProperties(properties);
if (generateGitPropertiesFile) {
@@ -322,30 +315,6 @@ public void execute() throws MojoExecutionException {
}
- private void filterNot(Properties properties, @Nullable List exclusions) {
- if (exclusions == null)
- return;
-
- List> excludePredicates = Lists.transform(exclusions, new Function>() {
- @Override
- public Predicate apply(String exclude) {
- return Predicates.containsPattern(exclude);
- }
- });
-
- Predicate shouldExclude = Predicates.alwaysFalse();
- for (Predicate predicate : excludePredicates) {
- shouldExclude = Predicates.or(shouldExclude, predicate);
- }
-
- for (String key : properties.stringPropertyNames()) {
- if (shouldExclude.apply(key)) {
- System.out.println("shouldExclude.apply(" + key +") = " + shouldExclude.apply(key));
- properties.remove(key);
- }
- }
- }
-
/**
* Reacts to an exception based on the {@code failOnUnableToExtractRepoInfo} setting.
* If it's true, an MojoExecutionException will be throw, otherwise we just log an error message.
@@ -361,7 +330,7 @@ private void handlePluginFailure(Exception e) throws MojoExecutionException {
}
}
- private void appendPropertiesToReactorProjects(@NotNull Properties properties) {
+ private void appendPropertiesToReactorProjects(@Nonnull Properties properties) {
for (MavenProject mavenProject : reactorProjects) {
Properties mavenProperties = mavenProject.getProperties();
@@ -399,7 +368,7 @@ private Properties initProperties() throws MojoExecutionException {
}
}
- private void logProperties(@NotNull Properties properties) {
+ private void logProperties(@Nonnull Properties properties) {
for (Object key : properties.keySet()) {
String keyString = key.toString();
if (isOurProperty(keyString)) {
@@ -408,120 +377,13 @@ private void logProperties(@NotNull Properties properties) {
}
}
- private boolean isOurProperty(@NotNull String keyString) {
+ private boolean isOurProperty(@Nonnull String keyString) {
return keyString.startsWith(prefixDot);
}
- void loadBuildTimeData(@NotNull Properties properties) {
- Date commitDate = new Date();
- SimpleDateFormat smf = new SimpleDateFormat(dateFormat);
- put(properties, BUILD_TIME, smf.format(commitDate));
- }
-
- void loadGitData(@NotNull Properties properties) throws IOException, MojoExecutionException {
- Repository git = getGitRepository();
- ObjectReader objectReader = git.newObjectReader();
-
- // git.user.name
- String userName = git.getConfig().getString("user", null, "name");
- put(properties, BUILD_AUTHOR_NAME, userName);
-
- // git.user.email
- String userEmail = git.getConfig().getString("user", null, "email");
- put(properties, BUILD_AUTHOR_EMAIL, userEmail);
-
- // more details parsed out bellow
- Ref HEAD = git.getRef(Constants.HEAD);
- if (HEAD == null) {
- throw new MojoExecutionException("Could not get HEAD Ref, are you sure you've set the dotGitDirectory property of this plugin to a valid path?");
- }
- RevWalk revWalk = new RevWalk(git);
- RevCommit headCommit = revWalk.parseCommit(HEAD.getObjectId());
- revWalk.markStart(headCommit);
-
- try {
- // git.branch
- String branch = determineBranchName(git, System.getenv());
- put(properties, BRANCH, branch);
-
- // git.commit.id.describe
- maybePutGitDescribe(properties, git);
-
- // git.commit.id
- put(properties, COMMIT_ID, headCommit.getName());
-
- // git.commit.id.abbrev
- putAbbrevCommitId(objectReader, properties, headCommit, abbrevLength);
-
- // git.commit.author.name
- String commitAuthor = headCommit.getAuthorIdent().getName();
- put(properties, COMMIT_AUTHOR_NAME, commitAuthor);
-
- // git.commit.author.email
- String commitEmail = headCommit.getAuthorIdent().getEmailAddress();
- put(properties, COMMIT_AUTHOR_EMAIL, commitEmail);
-
- // git commit.message.full
- String fullMessage = headCommit.getFullMessage();
- put(properties, COMMIT_MESSAGE_FULL, fullMessage);
-
- // git commit.message.short
- String shortMessage = headCommit.getShortMessage();
- put(properties, COMMIT_MESSAGE_SHORT, shortMessage);
-
- long timeSinceEpoch = headCommit.getCommitTime();
- Date commitDate = new Date(timeSinceEpoch * 1000); // git is "by sec" and java is "by ms"
- SimpleDateFormat smf = new SimpleDateFormat(dateFormat);
- put(properties, COMMIT_TIME, smf.format(commitDate));
-
- // git remote.origin.url
- String remoteOriginUrl = git.getConfig().getString("remote", "origin", "url");
- put(properties, REMOTE_ORIGIN_URL, remoteOriginUrl);
- } finally {
- revWalk.dispose();
- }
- }
-
- private void putAbbrevCommitId(ObjectReader objectReader, Properties properties, RevCommit headCommit, int abbrevLength) throws MojoExecutionException {
- if (abbrevLength < 2 || abbrevLength > 40) {
- throw new MojoExecutionException("Abbreviated commit id lenght must be between 2 and 40, inclusive! Was [%s]. ".codePointBefore(abbrevLength) +
- "Please fix your configuration (the element).");
- }
-
- try {
- AbbreviatedObjectId abbreviatedObjectId = objectReader.abbreviate(headCommit, abbrevLength);
- put(properties, COMMIT_ID_ABBREV, abbreviatedObjectId.name());
- } catch (IOException e) {
- throw new MojoExecutionException("Unable to abbreviate commit id! " +
- "You may want to investigate the element in your configuration.", e);
- }
- }
-
- void maybePutGitDescribe(@NotNull Properties properties, @NotNull Repository repository) throws MojoExecutionException {
- if (gitDescribe == null || !gitDescribe.isSkip()) {
- putGitDescribe(properties, repository);
- }
- }
-
- @VisibleForTesting
- void putGitDescribe(@NotNull Properties properties, @NotNull Repository repository) throws MojoExecutionException {
- try {
- DescribeResult describeResult = DescribeCommand
- .on(repository)
- .withLoggerBridge(loggerBridge)
- .setVerbose(verbose)
- .apply(gitDescribe)
- .call();
-
- put(properties, COMMIT_DESCRIBE, describeResult.toString());
- } catch (GitAPIException ex) {
- throw new MojoExecutionException("Unable to obtain git.commit.id.describe information", ex);
- }
- }
-
static int counter;
- void generatePropertiesFile(@NotNull Properties properties, File base, String propertiesFilename) throws IOException {
+ void generatePropertiesFile(@Nonnull Properties properties, File base, String propertiesFilename) throws IOException {
FileWriter fileWriter = null;
File gitPropsFile = new File(base, propertiesFilename);
try {
@@ -540,53 +402,14 @@ void generatePropertiesFile(@NotNull Properties properties, File base, String pr
} catch (IOException ex) {
throw new RuntimeException("Cannot create custom git properties file: " + gitPropsFile, ex);
} finally {
- Closeables.closeQuietly(fileWriter);
+ Closeables.close(fileWriter, true);
}
}
- boolean isPomProject(@NotNull MavenProject project) {
+ boolean isPomProject(@Nonnull MavenProject project) {
return project.getPackaging().equalsIgnoreCase("pom");
}
- @NotNull
- private Repository getGitRepository() throws MojoExecutionException {
- Repository repository;
-
- FileRepositoryBuilder repositoryBuilder = new FileRepositoryBuilder();
- try {
- repository = repositoryBuilder
- .setGitDir(dotGitDirectory)
- .readEnvironment() // scan environment GIT_* variables
- .findGitDir() // scan up the file system tree
- .build();
- } catch (IOException e) {
- throw new MojoExecutionException("Could not initialize repository...", e);
- }
-
- if (repository == null) {
- throw new MojoExecutionException("Could not create git repository. Are you sure '" + dotGitDirectory + "' is the valid Git root for your project?");
- }
-
- return repository;
- }
-
- private void put(@NotNull Properties properties, String key, String value) {
- putWithoutPrefix(properties, prefixDot + key, value);
- }
-
- private void putWithoutPrefix(@NotNull Properties properties, String key, String value) {
- if (!isNotEmpty(value)) {
- value = "Unknown";
- }
-
- log(key, value);
- properties.put(key, value);
- }
-
- private boolean isNotEmpty(@Nullable String value) {
- return null != value && !" ".equals(value.trim().replaceAll(" ", ""));
- }
-
void log(String... parts) {
loggerBridge.log((Object[]) parts);
}
@@ -599,50 +422,6 @@ private boolean directoryDoesNotExits(File fileLocation) {
return !directoryExists(fileLocation);
}
- /**
- * If running within Jenkins/Hudosn, honor the branch name passed via GIT_BRANCH env var. This
- * is necessary because Jenkins/Hudson alwways invoke build in a detached head state.
- *
- * @param git
- * @param env
- * @return results of git.getBranch() or, if in Jenkins/Hudson, value of GIT_BRANCH
- */
- protected String determineBranchName(Repository git, Map env) throws IOException {
- if (runningOnBuildServer(env)) {
- return determineBranchNameOnBuildServer(git, env);
- } else {
- return git.getBranch();
- }
- }
-
- /**
- * Is "Jenkins aware", and prefers {@code GIT_BRANCH} to getting the branch via git if that enviroment variable is set.
- * The {@GIT_BRANCH} variable is set by Jenkins/Hudson when put in detached HEAD state, but it still knows which branch was cloned.
- */
- protected String determineBranchNameOnBuildServer(Repository git, Map env) throws IOException {
- String enviromentBasedBranch = env.get("GIT_BRANCH");
- if(isNullOrEmpty(enviromentBasedBranch)) {
- log("Detected that running on CI enviroment, but using repository branch, no GIT_BRANCH detected.");
- return git.getBranch();
- }else {
- log("Using environment variable based branch name.", "GIT_BRANCH =", enviromentBasedBranch);
- return enviromentBasedBranch;
- }
- }
-
- /**
- * Detects if we're running on Jenkins or Hudson, based on expected env variables.
- *
- * TODO: How can we detect Bamboo, TeamCity etc? Pull requests welcome.
- *
- * @return true if running
- * @see JenkinsSetEnvironmentVariables
- * @param env
- */
- private boolean runningOnBuildServer(Map env) {
- return env.containsKey("HUDSON_URL") || env.containsKey("JENKINS_URL");
- }
-
// SETTERS FOR TESTS ----------------------------------------------------
public void setFormat(String format) {
diff --git a/src/main/java/pl/project13/maven/git/GitDirLocator.java b/maven/src/main/java/pl/project13/maven/git/GitDirLocator.java
similarity index 92%
rename from src/main/java/pl/project13/maven/git/GitDirLocator.java
rename to maven/src/main/java/pl/project13/maven/git/GitDirLocator.java
index dad40d73..efe2a29e 100644
--- a/src/main/java/pl/project13/maven/git/GitDirLocator.java
+++ b/maven/src/main/java/pl/project13/maven/git/GitDirLocator.java
@@ -17,15 +17,21 @@
package pl.project13.maven.git;
-import com.google.common.base.Optional;
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.List;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
import org.apache.maven.artifact.Artifact;
import org.apache.maven.project.MavenProject;
import org.eclipse.jgit.lib.Constants;
-import org.jetbrains.annotations.NotNull;
-import org.jetbrains.annotations.Nullable;
-import java.io.*;
-import java.util.List;
+import com.google.common.base.Optional;
/**
* Encapsulates logic to locate a valid .git directory.
@@ -42,7 +48,7 @@ public GitDirLocator(MavenProject mavenProject, List reactorProjec
}
@Nullable
- public File lookupGitDirectory(@NotNull File manuallyConfiguredDir) {
+ public File lookupGitDirectory(@Nonnull File manuallyConfiguredDir) {
if (manuallyConfiguredDir.exists()) {
@@ -119,7 +125,7 @@ private File findProjectGitDirectory() {
*
* @return MavenProject parent project or NULL if no parent available
*/
- private Optional getReactorParentProject(@NotNull MavenProject project) {
+ private Optional getReactorParentProject(@Nonnull MavenProject project) {
Artifact parentArtifact = project.getParentArtifact();
if (parentArtifact != null) {
@@ -138,7 +144,7 @@ private Optional getReactorParentProject(@NotNull MavenProject pro
*
* @return File object with path loaded or null
*/
- private File processGitDirFile(@NotNull File file) {
+ private File processGitDirFile(@Nonnull File file) {
try {
BufferedReader reader = null;
@@ -171,8 +177,8 @@ private File processGitDirFile(@NotNull File file) {
}
}
- @NotNull
- private static File getProjectGitDir(@NotNull MavenProject mavenProject) {
+ @Nonnull
+ private static File getProjectGitDir(@Nonnull MavenProject mavenProject) {
// FIXME Shouldn't this look at the dotGitDirectory property (if set) for the given project?
return new File(mavenProject.getBasedir(), Constants.DOT_GIT);
}
diff --git a/src/main/java/pl/project13/maven/git/log/MavenLoggerBridge.java b/maven/src/main/java/pl/project13/maven/git/log/MavenLoggerBridge.java
similarity index 100%
rename from src/main/java/pl/project13/maven/git/log/MavenLoggerBridge.java
rename to maven/src/main/java/pl/project13/maven/git/log/MavenLoggerBridge.java
diff --git a/src/main/resources/META-INF/m2e/lifecycle-mapping-metadata.xml b/maven/src/main/resources/META-INF/m2e/lifecycle-mapping-metadata.xml
similarity index 100%
rename from src/main/resources/META-INF/m2e/lifecycle-mapping-metadata.xml
rename to maven/src/main/resources/META-INF/m2e/lifecycle-mapping-metadata.xml
diff --git a/src/main/resources/git-commit-id.properties b/maven/src/main/resources/git-commit-id.properties
similarity index 100%
rename from src/main/resources/git-commit-id.properties
rename to maven/src/main/resources/git-commit-id.properties
diff --git a/src/test/java/pl/project13/jgit/DescribeCommandAbbrevIntegrationTest.java b/maven/src/test/java/pl/project13/jgit/DescribeCommandAbbrevIntegrationTest.java
similarity index 98%
rename from src/test/java/pl/project13/jgit/DescribeCommandAbbrevIntegrationTest.java
rename to maven/src/test/java/pl/project13/jgit/DescribeCommandAbbrevIntegrationTest.java
index 8e06e4d1..20b5dc16 100644
--- a/src/test/java/pl/project13/jgit/DescribeCommandAbbrevIntegrationTest.java
+++ b/maven/src/test/java/pl/project13/jgit/DescribeCommandAbbrevIntegrationTest.java
@@ -34,15 +34,18 @@
package pl.project13.jgit;
-import com.google.common.base.Optional;
+import static org.fest.assertions.Assertions.assertThat;
+
+import javax.annotation.Nonnull;
+
import org.eclipse.jgit.lib.Repository;
-import org.jetbrains.annotations.NotNull;
import org.junit.Test;
+
import pl.project13.maven.git.AvailableGitTestRepo;
import pl.project13.maven.git.FileSystemMavenSandbox;
import pl.project13.maven.git.GitIntegrationTest;
-import static org.fest.assertions.Assertions.assertThat;
+import com.google.common.base.Optional;
public class DescribeCommandAbbrevIntegrationTest extends GitIntegrationTest {
@@ -123,7 +126,7 @@ public void onGitCommitIdsRepo_shouldNoticeThat2CharsIsTooLittleToBeUniqueAndUse
assertThat(res.prefixedCommitId()).isEqualTo("g" + smallestAbbrevGitWillUse);
}
- String abbrev(@NotNull String id, int n) {
+ String abbrev(@Nonnull String id, int n) {
return id.substring(0, n);
}
}
diff --git a/src/test/java/pl/project13/jgit/DescribeCommandIntegrationTest.java b/maven/src/test/java/pl/project13/jgit/DescribeCommandIntegrationTest.java
similarity index 99%
rename from src/test/java/pl/project13/jgit/DescribeCommandIntegrationTest.java
rename to maven/src/test/java/pl/project13/jgit/DescribeCommandIntegrationTest.java
index 686a63e9..789a8f7d 100644
--- a/src/test/java/pl/project13/jgit/DescribeCommandIntegrationTest.java
+++ b/maven/src/test/java/pl/project13/jgit/DescribeCommandIntegrationTest.java
@@ -17,24 +17,27 @@
package pl.project13.jgit;
-import com.google.common.base.Optional;
-import com.google.common.collect.ImmutableMap;
+import static java.util.Collections.singletonList;
+import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.spy;
+
+import javax.annotation.Nonnull;
+
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.ResetCommand;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
-import org.jetbrains.annotations.NotNull;
import org.junit.Test;
+
import pl.project13.maven.git.AvailableGitTestRepo;
import pl.project13.maven.git.FileSystemMavenSandbox;
import pl.project13.maven.git.GitIntegrationTest;
-import static java.util.Collections.singletonList;
-import static org.fest.assertions.Assertions.assertThat;
-import static org.mockito.Matchers.any;
-import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.spy;
+import com.google.common.base.Optional;
+import com.google.common.collect.ImmutableMap;
public class DescribeCommandIntegrationTest extends GitIntegrationTest {
@@ -444,11 +447,11 @@ public void trimFullTagName_shouldTrimFullTagNamePrefix() throws Exception {
assertThat(simpleName).isEqualTo("v1.0.0");
}
- String abbrev(@NotNull String id) {
+ String abbrev(@Nonnull String id) {
return abbrev(id, DEFAULT_ABBREV_LEN);
}
- String abbrev(@NotNull String id, int n) {
+ String abbrev(@Nonnull String id, int n) {
return id.substring(0, n);
}
}
diff --git a/src/test/java/pl/project13/jgit/DescribeCommandOptionsTest.java b/maven/src/test/java/pl/project13/jgit/DescribeCommandOptionsTest.java
similarity index 100%
rename from src/test/java/pl/project13/jgit/DescribeCommandOptionsTest.java
rename to maven/src/test/java/pl/project13/jgit/DescribeCommandOptionsTest.java
diff --git a/src/test/java/pl/project13/jgit/DescribeCommandTagsIntegrationTest.java b/maven/src/test/java/pl/project13/jgit/DescribeCommandTagsIntegrationTest.java
similarity index 100%
rename from src/test/java/pl/project13/jgit/DescribeCommandTagsIntegrationTest.java
rename to maven/src/test/java/pl/project13/jgit/DescribeCommandTagsIntegrationTest.java
diff --git a/src/test/java/pl/project13/jgit/DescribeResultTest.java b/maven/src/test/java/pl/project13/jgit/DescribeResultTest.java
similarity index 100%
rename from src/test/java/pl/project13/jgit/DescribeResultTest.java
rename to maven/src/test/java/pl/project13/jgit/DescribeResultTest.java
diff --git a/maven/src/test/java/pl/project13/jgit/GitDataLoaderTest.java b/maven/src/test/java/pl/project13/jgit/GitDataLoaderTest.java
new file mode 100644
index 00000000..dd636f67
--- /dev/null
+++ b/maven/src/test/java/pl/project13/jgit/GitDataLoaderTest.java
@@ -0,0 +1,57 @@
+package pl.project13.jgit;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Properties;
+
+import org.fest.assertions.Assertions;
+import org.junit.Before;
+import org.junit.Test;
+
+import pl.project13.maven.git.AvailableGitTestRepo;
+import pl.project13.maven.git.FileSystemMavenSandbox;
+import pl.project13.maven.git.GitDataLoader;
+import pl.project13.maven.git.GitDataLoader.GitDataLoaderBuilder;
+import pl.project13.maven.git.GitDataLoaderException;
+import pl.project13.maven.git.GitIntegrationTest;
+
+/**
+ * Tests for {@link GitDataLoader} and {@link GitDataLoaderBuilder}.
+ *
+ * @author jbellmann
+ *
+ */
+public class GitDataLoaderTest extends GitIntegrationTest {
+
+ final static String PROJECT_NAME = "my-jar-project";
+ final int ABBREV_LENGTH = 8;
+
+ @Override
+ @Before
+ public void setUp() throws Exception {
+ super.setUp();
+
+ mavenSandbox
+ .withParentProject(PROJECT_NAME, "jar")
+ .withNoChildProject()
+ .withGitRepoInParent(AvailableGitTestRepo.WITH_LIGHTWEIGHT_TAG_BEFORE_ANNOTATED_TAG)
+ .create(FileSystemMavenSandbox.CleanUp.CLEANUP_FIRST);
+ }
+
+ @Test
+ public void test() throws IOException, GitDataLoaderException{
+ System.out.println("WHERE IS THE REPOSITORY?");
+ String path = mavenSandbox.getSandboxDir().getAbsolutePath();
+ System.out.println(path);
+ File workingTreeDirectory = mavenSandbox.getSandboxDir().getAbsoluteFile();
+
+ GitDataLoaderBuilder dataLoaderBuilder = new GitDataLoaderBuilder();
+ GitDataLoader gitDataLoader = dataLoaderBuilder.withWorkingTreeDirectory(workingTreeDirectory).withAbbrevationLength(ABBREV_LENGTH).build();
+ Properties props = new Properties();
+ gitDataLoader.loadGitData(props);
+ System.out.println(props);
+ String abbrev = props.getProperty("gitcommit.id.abbrev");
+ Assertions.assertThat(abbrev.length()).isEqualTo(ABBREV_LENGTH);
+ }
+
+}
diff --git a/src/test/java/pl/project13/maven/git/AvailableGitTestRepo.java b/maven/src/test/java/pl/project13/maven/git/AvailableGitTestRepo.java
similarity index 98%
rename from src/test/java/pl/project13/maven/git/AvailableGitTestRepo.java
rename to maven/src/test/java/pl/project13/maven/git/AvailableGitTestRepo.java
index 48f195ce..bdab0b9f 100644
--- a/src/test/java/pl/project13/maven/git/AvailableGitTestRepo.java
+++ b/maven/src/test/java/pl/project13/maven/git/AvailableGitTestRepo.java
@@ -17,10 +17,10 @@
package pl.project13.maven.git;
-import org.jetbrains.annotations.NotNull;
-
import java.io.File;
+import javax.annotation.Nonnull;
+
public enum AvailableGitTestRepo {
WITH_ONE_COMMIT("src/test/resources/_git_one_commit"),
WITH_ONE_COMMIT_DIRTY("src/test/resources/_git_one_commit_dirty"),
@@ -64,7 +64,7 @@ public enum AvailableGitTestRepo {
this.dir = dir;
}
- @NotNull
+ @Nonnull
public File getDir() {
return new File(dir);
}
diff --git a/src/test/java/pl/project13/maven/git/ContainsKeyCondition.java b/maven/src/test/java/pl/project13/maven/git/ContainsKeyCondition.java
similarity index 93%
rename from src/test/java/pl/project13/maven/git/ContainsKeyCondition.java
rename to maven/src/test/java/pl/project13/maven/git/ContainsKeyCondition.java
index 9d5616f3..f3d990af 100644
--- a/src/test/java/pl/project13/maven/git/ContainsKeyCondition.java
+++ b/maven/src/test/java/pl/project13/maven/git/ContainsKeyCondition.java
@@ -17,11 +17,12 @@
package pl.project13.maven.git;
-import org.fest.assertions.Condition;
-import org.jetbrains.annotations.NotNull;
-
import java.util.Map;
+import javax.annotation.Nonnull;
+
+import org.fest.assertions.Condition;
+
class ContainsKeyCondition extends Condition