Skip to content

Commit b66a148

Browse files
committed
TDEPS-276 Extract source paths from local pom.xml sources defined with build-helper-maven-plugin
1 parent f17c054 commit b66a148

3 files changed

Lines changed: 145 additions & 17 deletions

File tree

  • src
    • main/clojure/clojure/tools/deps/extensions
    • test/clojure/clojure/tools/deps/extensions
  • test-data/pom-extra-src

src/main/clojure/clojure/tools/deps/extensions/pom.clj

Lines changed: 66 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
[clojure.tools.deps.util.session :as session])
1717
(:import
1818
[java.io File]
19-
[java.util Properties]
19+
[java.util List Properties]
2020
;; maven-model
21-
[org.apache.maven.model Model Dependency Exclusion]
21+
[org.apache.maven.model Model Dependency Exclusion Plugin PluginExecution]
2222
;; maven-model-builder
2323
[org.apache.maven.model.building DefaultModelBuildingRequest DefaultModelBuilderFactory ModelSource FileModelSource]
2424
[org.apache.maven.model.resolution ModelResolver]
@@ -28,6 +28,8 @@
2828
[org.apache.maven.model Resource License]
2929
;; maven-core
3030
[org.apache.maven.project ProjectModelResolver ProjectBuildingRequest$RepositoryMerging]
31+
;; plexus-utils
32+
[org.codehaus.plexus.util.xml Xpp3Dom]
3133
))
3234

3335
(set! *warn-on-reflection* true)
@@ -99,29 +101,76 @@
99101
model (read-model-file pom config)]
100102
(model-deps model)))
101103

104+
;; Leiningen (and others) use this plugin to attach additional source dirs when the lifecycle
105+
;; executes. To avoid executing (which has security and runtime issues), this function
106+
;; statically extracts those attached dirs from something like this:
107+
;;
108+
;; <plugin>
109+
;; <groupId>org.codehaus.mojo</groupId>
110+
;; <artifactId>build-helper-maven-plugin</artifactId>
111+
;; <version>1.7</version>
112+
;; <executions>
113+
;; <execution>
114+
;; <id>add-source</id>
115+
;; <phase>generate-sources</phase>
116+
;; <goals>
117+
;; <goal>add-source</goal>
118+
;; </goals>
119+
;; <configuration>
120+
;; <sources>
121+
;; <source>extra</source> <!-- deps.edn can't see this otherwise -->
122+
;; </sources>
123+
;; </configuration>
124+
;; </execution>
125+
;; </executions>
126+
;; </plugin>
127+
;; In addition, this code also includes add-resource goals including resources
128+
(defn- get-build-helper-paths
129+
[^Model model]
130+
(let [plugins (some-> model .getBuild .getPlugins)
131+
build-helper-plugins (filter (fn [^Plugin plugin]
132+
(and (= "org.codehaus.mojo" (.getGroupId plugin))
133+
(= "build-helper-maven-plugin" (.getArtifactId plugin))))
134+
plugins)]
135+
(when (not (empty? build-helper-plugins))
136+
(let [^Plugin plugin (first plugins)
137+
extract-dirs (fn [^Plugin plugin goal ^String children]
138+
(->> (.getExecutions plugin)
139+
(filter (fn [^PluginExecution exec] (.contains ^List (.getGoals exec) goal)))
140+
(mapcat (fn [^PluginExecution exec]
141+
(let [^Xpp3Dom config (.getConfiguration exec)
142+
^Xpp3Dom sources (.getChild config children)]
143+
(map #(.getValue ^Xpp3Dom %) (.getChildren sources)))))))]
144+
(concat
145+
(extract-dirs plugin "add-source" "sources")
146+
(extract-dirs plugin "add-resource" "resources"))))))
147+
102148
(defmethod ext/coord-paths :pom
103149
[lib {:keys [deps/root] :as _coord} _mf config]
104-
(let [pom (jio/file root "pom.xml")
150+
(let [relativize (fn [^String s]
151+
(let [f (jio/file s)]
152+
(if (.isAbsolute f)
153+
(.getCanonicalPath f)
154+
(.getCanonicalPath (jio/file root f)))))
155+
pom (jio/file root "pom.xml")
105156
model (read-model-file pom config)
106157

107158
;; Maven core 3.8.2 returns an absolute directory here, which is a breaking regression
108159
;; from previous versions (see https://issues.apache.org/jira/browse/MNG-7218).
109160
;; Working around this with conditional code that deals with either absolute or relative.
110-
;; When MNG-7218 is fixed and deps bumped, might be able to revert the absolute path here.
161+
;; When MNG-7218 is fixed and deps bumped, might be able to revert the absolute path
162+
;; handling in relativize
111163
src-dir (jio/file (.. model getBuild getSourceDirectory))
112-
src-path (if (.isAbsolute src-dir)
113-
(.getCanonicalPath src-dir)
114-
(.getCanonicalPath (jio/file root src-dir)))
115-
116-
srcs (into [src-path
117-
(.getCanonicalPath (jio/file root "src/main/clojure"))]
118-
(for [^Resource resource (.. model getBuild getResources)]
119-
(let [dir (jio/file (.getDirectory resource))]
120-
(when dir
121-
(if (.isAbsolute dir)
122-
(.getCanonicalPath dir)
123-
(.getCanonicalPath (jio/file root dir)))))))]
124-
(->> srcs (remove nil?) distinct)))
164+
165+
resources (for [^Resource resource (.. model getBuild getResources)]
166+
(.getDirectory resource))
167+
168+
build-helper-srcs (get-build-helper-paths model)
169+
170+
srcs (concat [src-dir (.getCanonicalPath (jio/file root "src/main/clojure"))]
171+
resources
172+
build-helper-srcs)]
173+
(->> srcs (remove nil?) (map relativize) distinct)))
125174

126175
(defmethod ext/manifest-file :pom
127176
[_lib {:keys [deps/root] :as _coord} _mf _config]
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
(ns clojure.tools.deps.extensions.pom
2+
(:require [clojure.test :refer :all]
3+
[clojure.tools.deps.extensions :as ext]
4+
[clojure.tools.deps.extensions.maven]
5+
[clojure.tools.deps.extensions.pom]
6+
[clojure.tools.deps.util.maven :as maven])
7+
(:import [java.io File]))
8+
9+
(deftest tdeps-276-build-helper-src-dir
10+
(let [paths (ext/coord-paths 'foo/foo {:deps/root "test-data/pom-extra-src" :deps/manifest :pom}
11+
:pom {:mvn/repos maven/standard-repos})]
12+
(is (contains? (set paths) (.getAbsolutePath (File. "test-data/pom-extra-src/s1"))))
13+
(is (contains? (set paths) (.getAbsolutePath (File. "test-data/pom-extra-src/s2"))))
14+
(is (contains? (set paths) (.getAbsolutePath (File. "test-data/pom-extra-src/r1"))))
15+
(is (contains? (set paths) (.getAbsolutePath (File. "test-data/pom-extra-src/r2"))))))
16+
17+
(comment
18+
(tdeps-276-build-helper-src-dir)
19+
)

test-data/pom-extra-src/pom.xml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>foo</groupId>
5+
<artifactId>foo</artifactId>
6+
<packaging>jar</packaging>
7+
<version>0.1.0-SNAPSHOT</version>
8+
<name>foo</name>
9+
<build>
10+
<sourceDirectory>src</sourceDirectory>
11+
<testSourceDirectory>test</testSourceDirectory>
12+
<resources>
13+
<resource>
14+
<directory>resources</directory>
15+
</resource>
16+
</resources>
17+
<testResources>
18+
<testResource>
19+
<directory>resources</directory>
20+
</testResource>
21+
</testResources>
22+
<directory>target</directory>
23+
<outputDirectory>target/classes</outputDirectory>
24+
<plugins>
25+
<plugin>
26+
<groupId>org.codehaus.mojo</groupId>
27+
<artifactId>build-helper-maven-plugin</artifactId>
28+
<version>1.7</version>
29+
<executions>
30+
<execution>
31+
<id>add-source</id>
32+
<phase>generate-sources</phase>
33+
<goals>
34+
<goal>add-source</goal>
35+
</goals>
36+
<configuration>
37+
<sources>
38+
<source>s1</source> <!-- should be added to classpath -->
39+
<source>s2</source>
40+
</sources>
41+
</configuration>
42+
</execution>
43+
<execution>
44+
<id>add-resource</id>
45+
<phase>generate-resources</phase>
46+
<goals>
47+
<goal>add-resource</goal>
48+
</goals>
49+
<configuration>
50+
<resources>
51+
<resource>r1</resource> <!-- should be added to classpath -->
52+
<resource>r2</resource>
53+
</resources>
54+
</configuration>
55+
</execution>
56+
</executions>
57+
</plugin>
58+
</plugins>
59+
</build>
60+
</project>

0 commit comments

Comments
 (0)