Skip to content

Commit 5baca0a

Browse files
committed
New mojo parameter <customProperties> to evaluate arbitrary expressions based on build properties
1 parent 696628b commit 5baca0a

7 files changed

Lines changed: 41 additions & 59 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
<localRepositoryPath>${project.build.directory}/local-repo</localRepositoryPath>
9494
<preBuildHookScript>setup.groovy</preBuildHookScript>
9595
<postBuildHookScript>verify.groovy</postBuildHookScript>
96-
<debug>false</debug>
96+
<debug>true</debug>
9797
<properties>
9898
<!-- <clojure.version>${clojure.version}</clojure.version> -->
9999
</properties>

src/it/custom-properties/pom.xml

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,34 +24,14 @@
2424
<goals><goal>set-properties</goal></goals>
2525
<configuration>
2626
<tstampFormat>yyyy-MM-dd</tstampFormat>
27+
<customProperties>
28+
{ :build-tag-lowercase (clojure.string/lower-case build-tag) }
29+
</customProperties>
2730
</configuration>
2831
</execution>
2932
</executions>
3033
</plugin>
3134

32-
<plugin>
33-
<groupId>org.apache.maven.plugins</groupId>
34-
<artifactId>maven-antrun-plugin</artifactId>
35-
<executions>
36-
<execution>
37-
<id>echo-stuff</id>
38-
<phase>initialize</phase>
39-
<goals>
40-
<goal>run</goal>
41-
</goals>
42-
<configuration>
43-
<tasks>
44-
<echo message="build-version: ${build-version}" />
45-
<echo message="build-tag: ${build-tag}" />
46-
<echo message="build-tag-delta: ${build-tag-delta}" />
47-
<echo message="build-tstamp: ${build-tstamp}" />
48-
<echo message="build-commit: ${build-commit}" />
49-
<echo message="build-commit-abbrev: ${build-commit-abbrev}" />
50-
</tasks>
51-
</configuration>
52-
</execution>
53-
</executions>
54-
</plugin>
5535
</plugins>
5636
</build>
5737

src/it/custom-properties/verify.groovy

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
// looking for timestamp with custom format yyyy-MM-dd
44

55
def build_log = new File("target/it/custom-properties/build.log").text
6-
boolean success = (build_log =~ /build-tstamp: \d\d\d\d-\d\d-\d\d/)
7-
return success
6+
7+
boolean tstamp = (build_log =~ /build-tstamp: \d\d\d\d-\d\d-\d\d/)
8+
9+
boolean customLowerCaseProperty = ( build_log =~ /build-tag-lowercase: n\/a/ )
10+
11+
return tstamp && customLowerCaseProperty
812

src/it/simple-project/pom.xml

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,6 @@
2626
</executions>
2727
</plugin>
2828

29-
<plugin>
30-
<groupId>org.apache.maven.plugins</groupId>
31-
<artifactId>maven-antrun-plugin</artifactId>
32-
<executions>
33-
<execution>
34-
<id>echo-stuff</id>
35-
<phase>initialize</phase>
36-
<goals>
37-
<goal>run</goal>
38-
</goals>
39-
<configuration>
40-
<tasks>
41-
<echo message="build-version: ${build-version}" />
42-
<echo message="build-tag: ${build-tag}" />
43-
<echo message="build-tag-delta: ${build-tag-delta}" />
44-
<echo message="build-tstamp: ${build-tstamp}" />
45-
<echo message="build-commit: ${build-commit}" />
46-
<echo message="build-commit-abbrev: ${build-commit-abbrev}" />
47-
</tasks>
48-
</configuration>
49-
</execution>
50-
</executions>
51-
</plugin>
5229
</plugins>
5330
</build>
5431

src/main/clojure/buildversion_plugin/git.clj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ and :git-tag-delta (number of commits -couting on first-parent paths only- from
3737
(sh/destroy p)
3838
{:git-tag tag, :git-tag-delta delta}))
3939

40-
(defn infer-project-version [dir {tstamp-format "tstamp-format"}]
40+
(defn infer-project-version [dir config-map]
4141
"Infer the current project version from tags on the source-control system"
4242

43-
(let [commit-tstamp (-> (run-git-wait dir "log -n 1 --format='%ct'")
43+
(let [tstamp-format (:tstamp-format config-map)
44+
commit-tstamp (-> (run-git-wait dir "log -n 1 --format='%ct'")
4445
trim-newline
4546
(Long/parseLong)
4647
(* 1000)

src/main/clojure/buildversion_plugin/mojo.clj

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,48 @@
44
(:require [clojure.maven.mojo.log :as log]
55
[buildversion-plugin.git :as git]))
66

7+
8+
(defn- eval-custom-script [properties snippet-str]
9+
(let [props-as-bindings (vec (mapcat
10+
(fn [[k v]] [(symbol (name k)) v])
11+
properties))
12+
snippet (read-string snippet-str)]
13+
14+
(eval `(let ~props-as-bindings ~snippet))))
15+
16+
717
(defmojo BuildVersionMojo
818

919
{:goal "set-properties" :phase "initialize" }
1020

1121
;; Mojo parameters
22+
1223
[project {:expression "${project}" :required true :readonly true}
24+
1325
tstamp-format {:alias "tstampFormat" :default "yyyyMMddHHmmss"
14-
:typename "java.lang.String"} ]
26+
:typename "java.lang.String"}
1527

28+
custom-script {:alias "customProperties" :typename "java.lang.String"} ]
1629

17-
;; Goal execution
18-
(let [versions-map (git/infer-project-version "." {"tstamp-format" tstamp-format})
19-
props (.getProperties project)]
2030

21-
(log/debug (str "buildnumber-plugin - Setting properties: "))
31+
;; Goal execution
32+
(let [git-versions (git/infer-project-version "." {:tstamp-format tstamp-format} )
33+
custom-versions (if custom-script
34+
(eval-custom-script git-versions custom-script)
35+
{})
36+
versions-map (merge git-versions custom-versions)
37+
props (.getProperties project)
38+
]
39+
40+
(log/warn (str "buildversion-plugin **** custom-versions map: " (pr-str custom-versions )))
41+
(log/warn (str "buildversion-plugin **** versions-map : " (pr-str versions-map )))
42+
(log/debug (str "buildversion-plugin - Setting properties: "))
2243
(doseq [[prop value] versions-map]
23-
(log/debug (str (name prop) "=" value))
44+
(log/debug (str (name prop) ": " value))
2445
(.put props (name prop) value))))
2546

2647

2748

2849

2950
;; injecting project version does not working well :-(
3051
;; (.setVersion project (:maven-artifact-version versions-map))
31-

src/test/clojure/buildversion_plugin/test/git.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@
152152

153153
(deftest test-tstamp-format-option
154154
(let [actual-versions (git/infer-project-version sample-project-dir
155-
{ "tstamp-format" "SSS"})]
155+
{ :tstamp-format "SSS"})]
156156
(println actual-versions)
157157
(is (re-find #"000" (:build-tstamp actual-versions))
158158
"Always expecting 000 milliseconds. Git precision is up to seconds")))

0 commit comments

Comments
 (0)