Constrain plugin dependencies
[odlparent.git] / features-parent / pom.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!-- vi: set et smarttab sw=4 tabstop=4: -->
3 <!--
4  Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
5
6  This program and the accompanying materials are made available under the
7  terms of the Eclipse Public License v1.0 which accompanies this distribution,
8  and is available at http://www.eclipse.org/legal/epl-v10.html
9 -->
10 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
11
12     <!--
13          Base parent pom for building, packaging and testing Karaf features.
14          Users should do the following:
15
16          - specify this as their parent
17          - define their features template in src/main/features
18          - make sure the packaging is set to jar
19          - setup projects <dependencies/> as needed for their features
20     -->
21
22     <modelVersion>4.0.0</modelVersion>
23     <parent>
24         <groupId>org.opendaylight.odlparent</groupId>
25         <artifactId>odlparent</artifactId>
26         <version>1.7.0-SNAPSHOT</version>
27         <relativePath>../odlparent</relativePath>
28     </parent>
29
30     <artifactId>features-parent</artifactId>
31     <packaging>pom</packaging>
32
33     <properties>
34         <features.file>features.xml</features.file>
35         <skip.karaf.featureTest>false</skip.karaf.featureTest>
36     </properties>
37
38     <build>
39         <resources>
40             <resource>
41                 <directory>src/main/resources</directory>
42                 <filtering>true</filtering>
43             </resource>
44         </resources>
45
46         <pluginManagement>
47             <plugins>
48                 <!-- generate dependencies versions -->
49                 <plugin>
50                     <artifactId>maven-dependency-plugin</artifactId>
51                     <version>2.10</version>
52                       <executions>
53                           <execution>
54                               <phase>generate-resources</phase>
55                               <goals><goal>resolve</goal></goals>
56                               <configuration>
57                                   <outputFile>${project.build.directory}/dependencies.txt</outputFile>
58                               </configuration>
59                           </execution>
60                       </executions>
61                 </plugin>
62                 <plugin>
63                     <groupId>com.alexecollins.maven.plugin</groupId>
64                     <artifactId>script-maven-plugin</artifactId>
65                     <version>1.0.0</version>
66                     <executions>
67                         <execution>
68                         <id>add-version-to-features</id>
69                         <phase>generate-resources</phase>
70                             <goals>
71                                  <goal>execute</goal>
72                             </goals>
73                             <configuration>
74                                 <language>groovy</language>
75                                 <script>
76                                     /**
77                                      * Placeholder, which is used in src/features/features.xml
78                                      * to mark version which should be inserted from dependencies.
79                                      * Currently works only for bundle and configfile tags
80                                      * with mvn: url schema, and needs to be used
81                                      * as third component of schema.
82                                      * eg. mvn:group/artefact/{{VERSION}}
83                                      */
84                                     def versionPlaceholder = "{{VERSION}}"
85                                     /**
86                                      * Path to features.xml which uses versionPlaceholder.
87                                      * This will be processed by this script.
88                                      *
89                                      */
90                                     def featureFilePath = "src/main/features/features.xml"
91                                     // Contains mapping of groupID:artefactID to versoin
92                                     def versionMap = new HashMap();
93                                     /* Loads transitive dependency list generated from
94                                      * maven-dependency-plugin resolve goal
95                                      * and populates map
96                                      */
97                                     def dependencies = new File(project.build.directory,"dependencies.txt")
98                                     dependencies.eachLine {
99                                         def cmps = it.trim().split(":")
100                                         // Depends on number of components:
101                                         //  - groupId
102                                         //  - artifactId
103                                         //  - Type
104                                         //  - Qualifier (optional)
105                                         //  - Version
106                                         //  - Scope
107                                         if(cmps.length > 4) {
108                                             def id = cmps[0] + ":" + cmps[1]
109                                             if(cmps.length == 6) {
110                                                 versionMap[id] = cmps[4]
111                                             } else if(cmps.length == 5) {
112                                                 versionMap[id] = cmps[3]
113                                             }
114                                         }
115                                     }
116
117                                     /*
118                                      * Takes splitted mvn: URL, looks for placeholder
119                                      * and returns new mvn: URL with version learned
120                                      * from dependency plugin.
121                                      *
122                                      * If referenced bundle is not dependency (direct or transitive)
123                                      * throws an exception and fails build.
124                                      *
125                                      */
126                                     def updatedURLFromProject = { args ->
127                                         // 0 - groupID, 1 - artifactID
128                                         // 2 - version, 3 - type, 4 - Classifier
129
130                                         def groupId = args[0];
131                                         def artifactId = args[1];
132                                         def id = groupId + ":" + artifactId
133                                         def dependencyVersion = versionMap[id]
134                                         if(dependencyVersion != null) {
135                                             // Overriding version
136                                             args[2] = dependencyVersion
137                                             return "mvn:" + args.join("/")
138                                         }
139                                         throw new IllegalArgumentException("Feature dependency $groupId:$artifactId is not a dependency of the project.")
140                                     }
141
142                                     def updateMavenDependency = { dep ->
143                                         def mvnUrl = dep.text()
144                                         def prefix = ""
145                                         if (mvnUrl.startsWith("wrap:")) {
146                                             prefix = "wrap:"
147                                             mvnUrl = mvnUrl.substring(5)
148                                         }
149                                         if (mvnUrl.startsWith("mvn:")) {
150                                             def components =  mvnUrl.substring(4).split("/")
151                                             if (components[2].startsWith(versionPlaceholder)) {
152                                                 def suffix = "";
153                                                 if (components[2].length() > versionPlaceholder.length()) {
154                                                     suffix = components[2].substring(versionPlaceholder.length())
155                                                     components[2] = versionPlaceholder
156                                                 }
157                                                 dep.value = prefix + updatedURLFromProject(components) + suffix
158                                             }
159                                         }
160                                     }
161
162                                     def featureFile = new File(project.basedir,featureFilePath)
163                                     def root = new XmlParser().parse(featureFile)
164
165                                     println "[INFO] Updating repository declarations"
166                                     root.repository.each updateMavenDependency
167
168                                     root.feature.each { feature ->
169                                         println "[INFO] Processing feature: ${feature.@name}"
170                                         feature.bundle.each updateMavenDependency
171                                         feature.configfile.each updateMavenDependency
172                                     }
173
174                                     def outDir = new File(project.build.directory,"generated-resources/script")
175                                     outDir.mkdirs();
176                                     def outFile = new File(outDir,"features.xml")
177                                     def outWriter = outFile.newPrintWriter("ASCII");
178                                     xmlPrinter = new XmlNodePrinter(outWriter);
179                                     xmlPrinter.preserveWhitespace = true
180                                     xmlPrinter.print(root)
181                                     outWriter.close();
182                                 </script>
183                             </configuration>
184                         </execution>
185                     </executions>
186                     <dependencies>
187                         <dependency>
188                             <groupId>org.codehaus.groovy</groupId>
189                             <artifactId>groovy</artifactId>
190                             <version>1.8.6</version>
191                         </dependency>
192                     </dependencies>
193                 </plugin>
194                 <plugin>
195                     <groupId>org.apache.karaf.tooling</groupId>
196                     <artifactId>karaf-maven-plugin</artifactId>
197                     <version>${karaf.version}</version>
198                     <extensions>true</extensions>
199                     <executions>
200                         <execution>
201                             <id>features-create-kar</id>
202                             <goals>
203                                 <goal>features-create-kar</goal>
204                             </goals>
205                             <configuration>
206                                 <featuresFile>${project.build.directory}/classes/${features.file}</featuresFile>
207                             </configuration>
208                         </execution>
209                     </executions>
210                     <!-- There is no useful configuration for the kar mojo. The features-generate-descriptor mojo configuration may be useful -->
211                 </plugin>
212                 <plugin>
213                     <groupId>org.codehaus.mojo</groupId>
214                     <artifactId>build-helper-maven-plugin</artifactId>
215                     <version>1.9.1</version>
216                     <executions>
217                         <execution>
218                         <phase>generate-resources</phase>
219                         <goals><goal>add-resource</goal></goals>
220                         <configuration>
221                             <resources>
222                               <resource>
223                                 <directory>${project.build.directory}/generated-resources/script</directory>
224                                 <filtering>true</filtering>
225                               </resource>
226                             </resources>
227                         </configuration>
228                         </execution>
229                         <execution>
230                             <id>attach-artifacts</id>
231                             <phase>package</phase>
232                             <goals>
233                                 <goal>attach-artifact</goal>
234                             </goals>
235                             <configuration>
236                                 <artifacts>
237                                     <artifact>
238                                         <file>${project.build.directory}/classes/${features.file}</file>
239                                         <type>xml</type>
240                                         <classifier>features</classifier>
241                                     </artifact>
242                                 </artifacts>
243                             </configuration>
244                         </execution>
245                     </executions>
246                 </plugin>
247                 <plugin>
248                     <artifactId>maven-resources-plugin</artifactId>
249                     <version>2.7</version>
250                     <executions>
251                         <execution>
252                             <id>filter</id>
253                             <phase>generate-resources</phase>
254                             <goals>
255                                 <goal>resources</goal>
256                             </goals>
257                         </execution>
258                     </executions>
259                 </plugin>
260                 <plugin>
261                     <artifactId>maven-surefire-plugin</artifactId>
262                     <version>${maven.surefire.version}</version>
263                     <configuration>
264                         <skip>${skip.karaf.featureTest}</skip>
265                         <dependenciesToScan>
266                             <dependency>org.opendaylight.odlparent:features-test</dependency>
267                         </dependenciesToScan>
268                     </configuration>
269                 </plugin>
270                 <!-- Ignore/Execute plugin execution -->
271                 <plugin>
272                   <groupId>org.eclipse.m2e</groupId>
273                   <artifactId>lifecycle-mapping</artifactId>
274                   <version>1.0.0</version>
275                   <configuration>
276                     <lifecycleMappingMetadata>
277                       <pluginExecutions>
278                         <pluginExecution>
279                           <pluginExecutionFilter>
280                             <groupId>com.alexecollins.maven.plugin</groupId>
281                             <artifactId>script-maven-plugin</artifactId>
282                             <versionRange>[0.0,)</versionRange>
283                             <goals>
284                               <goal>execute</goal>
285                             </goals>
286                           </pluginExecutionFilter>
287                           <action>
288                             <ignore/>
289                           </action>
290                         </pluginExecution>
291                         <pluginExecution>
292                           <pluginExecutionFilter>
293                             <artifactId>maven-dependency-plugin</artifactId>
294                             <versionRange>[0.0,)</versionRange>
295                             <goals>
296                               <goal>resolve</goal>
297                             </goals>
298                           </pluginExecutionFilter>
299                           <action>
300                             <ignore/>
301                           </action>
302                         </pluginExecution>
303                      </pluginExecutions>
304                    </lifecycleMappingMetadata>
305                   </configuration>
306                 </plugin>
307             </plugins>
308         </pluginManagement>
309
310         <plugins>
311             <plugin>
312                 <artifactId>maven-dependency-plugin</artifactId>
313             </plugin>
314             <plugin>
315                 <groupId>com.alexecollins.maven.plugin</groupId>
316                 <artifactId>script-maven-plugin</artifactId>
317             </plugin>
318             <plugin>
319                 <groupId>org.apache.karaf.tooling</groupId>
320                 <artifactId>karaf-maven-plugin</artifactId>
321             </plugin>
322             <plugin>
323                 <groupId>org.codehaus.mojo</groupId>
324                 <artifactId>build-helper-maven-plugin</artifactId>
325             </plugin>
326             <plugin>
327                 <artifactId>maven-resources-plugin</artifactId>
328             </plugin>
329             <plugin>
330                 <artifactId>maven-surefire-plugin</artifactId>
331             </plugin>
332         </plugins>
333     </build>
334
335     <dependencies>
336         <!-- test the features.xml -->
337         <dependency>
338             <groupId>org.opendaylight.odlparent</groupId>
339             <artifactId>features-test</artifactId>
340             <version>1.7.0-SNAPSHOT</version>
341             <scope>test</scope>
342         </dependency>
343     </dependencies>
344
345   <!--
346     Maven Site Configuration
347
348     The following configuration is necessary for maven-site-plugin to
349     correctly identify the correct deployment path for OpenDaylight Maven
350     sites.
351   -->
352   <url>${odl.site.url}/${project.groupId}/${stream}/${project.artifactId}/</url>
353
354   <distributionManagement>
355     <site>
356       <id>opendaylight-site</id>
357       <url>${nexus.site.url}/${project.artifactId}/</url>
358     </site>
359   </distributionManagement>
360 </project>