Skip to content

Commit

Permalink
write-pom - add new :pom-data that can include arbitrary pom data whe…
Browse files Browse the repository at this point in the history
…n generating a new pom, such as license info
  • Loading branch information
puredanger committed Oct 6, 2023
1 parent 2e54897 commit 35cedff
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 29 deletions.
16 changes: 14 additions & 2 deletions src/main/clojure/clojure/tools/build/api.clj
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,18 @@
See: https://maven.apache.org/pom.html#SCM for details
:src-dirs - coll of src dirs
:resource-dirs - coll of resource dirs
:repos - map of repo name to repo config, replaces repos from deps.edn"
:repos - map of repo name to repo config, replaces repos from deps.edn
:pom-data - vector of hiccup-style extra pom top elements to include when
no :src-pom is provided:
[[:licenses
[:license
[:name \"Eclipse Public License 1.0\"]
[:url \"https://opensource.org/license/epl-1-0/\"]
[:distribution \"repo\"]]]
[:organization \"Super Corp\"]]
The pom-data MUST NOT include:
:modelVersion, :packaging, :groupId, :artifactId, :version, :name,
:deps, :repositories, :build, or :scm"
[params]
(assert-required "write-pom" params [:basis :lib :version])
(assert-specs "write-pom" params
Expand All @@ -396,7 +407,8 @@
:version string?
:scm map?
:src-dirs ::specs/paths
:resource-dirs ::specs/paths)
:resource-dirs ::specs/paths
:pom-data vector?)
((requiring-resolve 'clojure.tools.build.tasks.write-pom/write-pom) params))

(defn jar
Expand Down
64 changes: 38 additions & 26 deletions src/main/clojure/clojure/tools/build/tasks/write_pom.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
[clojure.data.xml :as xml]
[clojure.data.xml.tree :as tree]
[clojure.data.xml.event :as event]
[clojure.walk :as walk]
[clojure.zip :as zip]
[clojure.tools.deps.util.maven :as maven]
[clojure.tools.deps.util.io :refer [printerrln]]
Expand All @@ -23,6 +24,8 @@
[java.util Date]))

(xml/alias-uri 'pom "http://maven.apache.org/POM/4.0.0")
(def ^:private pom-ns (name (.-name ^clojure.lang.Namespace (get (ns-aliases *ns*) 'pom))))


(defn- to-dep
[[lib {:keys [mvn/version exclusions optional] :as coord}]]
Expand Down Expand Up @@ -88,35 +91,43 @@
[::pom/repositories
(map to-repo repos)])

(defn- pomify
[val]
(if (and (vector? val) (keyword? (first val)))
(into [(keyword pom-ns (name (first val)))] (rest val))
val))

(defn- gen-pom
[{:keys [deps src-paths resource-paths repos group artifact version scm]
[{:keys [deps src-paths resource-paths repos group artifact version scm pom-data]
:or {version "0.1.0"}}]
(let [[path & paths] src-paths
{:keys [connection developerConnection tag url]} scm]
(xml/sexp-as-element
[::pom/project
{:xmlns "http://maven.apache.org/POM/4.0.0"
(keyword "xmlns:xsi") "http://www.w3.org/2001/XMLSchema-instance"
(keyword "xsi:schemaLocation") "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"}
[::pom/modelVersion "4.0.0"]
[::pom/packaging "jar"]
[::pom/groupId group]
[::pom/artifactId artifact]
[::pom/version version]
[::pom/name artifact]
(gen-deps deps)
(when (or path (seq resource-paths))
(when (seq paths) (apply printerrln "Skipping paths:" paths))
[::pom/build
(when path (gen-source-dir path))
(when (seq resource-paths) (gen-resources resource-paths))])
(gen-repos repos)
(when scm
[::pom/scm
(when connection [::pom/connection connection])
(when developerConnection [::pom/developerConnection developerConnection])
(when tag [::pom/tag tag])
(when url [::pom/url url])])])))
(into
[::pom/project
{:xmlns "http://maven.apache.org/POM/4.0.0"
(keyword "xmlns:xsi") "http://www.w3.org/2001/XMLSchema-instance"
(keyword "xsi:schemaLocation") "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"}
[::pom/modelVersion "4.0.0"]
[::pom/packaging "jar"]
[::pom/groupId group]
[::pom/artifactId artifact]
[::pom/version version]
[::pom/name artifact]
(gen-deps deps)
(when (or path (seq resource-paths))
(when (seq paths) (apply printerrln "Skipping paths:" paths))
[::pom/build
(when path (gen-source-dir path))
(when (seq resource-paths) (gen-resources resource-paths))])
(gen-repos repos)
(when scm
[::pom/scm
(when connection [::pom/connection connection])
(when developerConnection [::pom/developerConnection developerConnection])
(when tag [::pom/tag tag])
(when url [::pom/url url])])]
(walk/postwalk pomify pom-data)))))

(defn- make-xml-element
[{:keys [tag attrs] :as node} children]
Expand Down Expand Up @@ -221,7 +232,7 @@

(defn write-pom
[params]
(let [{:keys [basis class-dir target src-pom lib version scm src-dirs resource-dirs repos]} params
(let [{:keys [basis class-dir target src-pom lib version scm src-dirs resource-dirs repos pom-data]} params
{:keys [libs]} basis
root-deps (libs->deps libs)
src-pom-file (api/resolve-path (or src-pom "pom.xml"))
Expand All @@ -246,7 +257,8 @@
:group (namespace lib)
:artifact (name lib)}
version (assoc :version version)
scm (assoc :scm scm))))
scm (assoc :scm scm)
pom-data (assoc :pom-data pom-data))))
pom-dir-file (file/ensure-dir
(cond
class-dir (jio/file (api/resolve-path class-dir) (meta-maven-path {:lib lib}))
Expand Down
24 changes: 23 additions & 1 deletion src/test/clojure/clojure/tools/build/tasks/test_pom.clj
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@
:version "1.2.3"
:target "target/output-pom.xml"
:src-dirs ["src"]
:src-pom "pom.xml"
:resource-dirs ["resources"]
:basis (api/create-basis nil)})
(is (.exists (jio/file (project-path "target/output-pom.xml"))))))
Expand All @@ -233,6 +232,29 @@
(catch Throwable t
(is true)))))

(deftest test-pom-data
(with-test-dir "test-data/p1"
(api/set-project-root! (.getAbsolutePath *test-dir*))
(api/delete {:path "target"})
(api/write-pom {:lib 'test/p1
:version "1.2.3"
:target "target"
:src-dirs ["src"]
:resource-dirs ["resources"]
:basis (api/create-basis nil)
:pom-data [[:licenses
[:license
[:name "Apache-2.0"]
[:url "https://www.apache.org/licenses/LICENSE-2.0.txt"]
[:distribution "repo"]
[:comments "OSS license"]]]
[:foo
[:bar "hello"]]]})
(is (.exists (jio/file (project-path "target/pom.xml"))))
(let [written-pom (slurp (project-path "target/pom.xml"))]
(is (str/includes? written-pom "<name>Apache-2.0</name>"))
(is (str/includes? written-pom "<bar>hello</bar>")))))

(comment
(run-tests)
(test-validate-lib)
Expand Down

0 comments on commit 35cedff

Please sign in to comment.