Introduce features-builder
[yangtools.git] / common / features-builder / 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     <modelVersion>4.0.0</modelVersion>
12     <parent>
13         <groupId>org.opendaylight.odlparent</groupId>
14         <artifactId>odlparent</artifactId>
15         <version>1.5.0-SNAPSHOT</version>
16     </parent>
17
18     <groupId>org.opendaylight.yangtools</groupId>
19     <artifactId>features-builder</artifactId>
20     <packaging>pom</packaging>
21     <version>0.7.0-SNAPSHOT</version>
22
23     <properties>
24         <features.file>features.xml</features.file>
25     </properties>
26
27     <build>
28         <resources>
29             <resource>
30                 <directory>src/main/resources</directory>
31                 <filtering>true</filtering>
32             </resource>
33         </resources>
34
35         <pluginManagement>
36             <plugins>
37                 <!-- generate dependencies versions -->
38                 <plugin>
39                     <artifactId>maven-dependency-plugin</artifactId>
40                       <executions>
41                           <execution>
42                               <phase>generate-resources</phase>
43                               <goals><goal>resolve</goal></goals>
44                               <configuration>
45                                   <outputFile>${project.build.directory}/dependencies.txt</outputFile>
46                               </configuration>
47                           </execution>
48                       </executions>
49                 </plugin>
50                 <plugin>
51                     <groupId>com.alexecollins.maven.plugin</groupId>
52                     <artifactId>script-maven-plugin</artifactId>
53                     <version>1.0.0</version>
54                     <executions>
55                         <execution>
56                         <id>add-version-to-features</id>
57                         <phase>generate-resources</phase>
58                             <goals>
59                                  <goal>execute</goal>
60                             </goals>
61                             <configuration>
62                                 <language>groovy</language>
63                                 <script>
64                                     /**
65                                      * Placeholder, which is used in src/feature/features.xml
66                                      * to mark version which should be inserted from dependencies.
67                                      * Currently works only for bundle and configfile tags
68                                      * with mvn: url schema, and needs to be used 
69                                      * as third component of schema.
70                                      * eg. mvn:group/artefact/{{VERSION}}
71                                      */
72                                     def versionPlaceholder = "{{VERSION}}"
73                                     /**
74                                      * Path to features.xml which uses versionPlaceholder.
75                                      * This will be processed by this script.
76                                      *
77                                      */
78                                     def featureFilePath = "src/main/feature/features.xml"
79                                     // Contains mapping of groupID:artefactID to versoin
80                                     def versionMap = new HashMap();
81                                     /* Loads transitive dependency list generated from
82                                      * maven-dependency-plugin resolve goal
83                                      * and populates map
84                                      */
85                                     def dependencies = new File(project.build.directory,"dependencies.txt")
86                                     dependencies.eachLine {
87                                         def cmps = it.trim().split(":")
88                                         // 0 - groupId
89                                         // 1 - artifactId
90                                         // 2 - Type
91                                         // 3 - Version
92                                         if(cmps.length >= 4) {
93                                             def id = cmps[0] + ":" + cmps[1]
94                                             versionMap[id] = cmps[3]
95                                         }
96                                     }
97
98                                     /*
99                                      * Takes splitted mvn: URL, looks for placeholder
100                                      * and returns new mvn: URL with version learned
101                                      * from dependency plugin.
102                                      *
103                                      * If referenced bundle is not dependency (direct or transitive)
104                                      * throws an exception and fails build.
105                                      *
106                                      */
107                                     def updatedURLFromProject = { args ->
108                                         // 0 - groupID, 1 - artifactID
109                                         // 2 - version, 3 - type, 4 - Classifier
110
111                                         def groupId = args[0];
112                                         def artifactId = args[1];
113                                         def id = groupId + ":" + artifactId
114                                         def dependencyVersion = versionMap[id]
115                                         if(dependencyVersion != null) {
116                                             // Overriding version
117                                             args[2] = dependencyVersion
118                                             return "mvn:" + args.join("/")
119                                         }
120                                         throw new IllegalArgumentException("Feature dependency $groupId:$artifactId is not dependecy of project.")
121                                     }
122
123
124                                     def updateMavenDependency  = { dep ->
125                                        def mvnUrl = dep.text()
126                                        if(mvnUrl.startsWith("mvn:")) {
127                                          def components =  mvnUrl.substring(4).split("/")
128                                          if(components[2] == versionPlaceholder) {
129                                          dep.value = updatedURLFromProject(components)
130                                          }
131                                        }
132                                     }
133
134                                     def featureFile = new File(project.basedir,featureFilePath)
135                                     def root = new XmlParser().parse(featureFile)
136
137                                     root.feature.each { feature ->
138                                         println "[INFO] Processing feature: ${feature.@name}"
139                                         feature.bundle.each updateMavenDependency
140                                         feature.configfile.each updateMavenDependency
141                                     }
142
143                                     def outDir = new File(project.build.directory,"generated-resources/script")
144                                     outDir.mkdirs();
145                                     def outFile = new File(outDir,"features.xml")
146                                     def outWriter = outFile.newPrintWriter("ASCII");
147                                     xmlPrinter = new XmlNodePrinter(outWriter);
148                                     xmlPrinter.preserveWhitespace = true
149                                     xmlPrinter.print(root)
150                                     outWriter.close();
151                                 </script>
152                             </configuration>
153                         </execution>
154                     </executions>
155                     <dependencies>
156                         <dependency>
157                             <groupId>org.codehaus.groovy</groupId>
158                             <artifactId>groovy</artifactId>
159                             <version>1.8.6</version>
160                         </dependency>
161                     </dependencies>
162                 </plugin>
163                 <plugin>
164                     <groupId>org.apache.karaf.tooling</groupId>
165                     <artifactId>karaf-maven-plugin</artifactId>
166                     <version>${karaf.version}</version>
167                     <extensions>true</extensions>
168                     <executions>
169                         <execution>
170                             <id>features-create-kar</id>
171                             <goals>
172                                 <goal>features-create-kar</goal>
173                             </goals>
174                             <configuration>
175                                 <featuresFile>${project.build.directory}/classes/${features.file}</featuresFile>
176                             </configuration>
177                         </execution>
178                     </executions>
179                     <!-- There is no useful configuration for the kar mojo. The features-generate-descriptor mojo configuration may be useful -->
180                 </plugin>
181                 <plugin>
182                     <groupId>org.codehaus.mojo</groupId>
183                     <artifactId>build-helper-maven-plugin</artifactId>
184                     <executions>
185                         <execution>
186                         <phase>generate-resources</phase>
187                         <goals><goal>add-resource</goal></goals>
188                         <configuration>
189                             <resources>
190                               <resource>
191                                 <directory>${project.build.directory}/generated-resources/script</directory>
192                                 <filtering>true</filtering>
193                               </resource>
194                             </resources>
195                         </configuration>
196                         </execution>
197                         <execution>
198                             <id>attach-artifacts</id>
199                             <phase>package</phase>
200                             <goals>
201                                 <goal>attach-artifact</goal>
202                             </goals>
203                             <configuration>
204                                 <artifacts>
205                                     <artifact>
206                                         <file>${project.build.directory}/classes/${features.file}</file>
207                                         <type>xml</type>
208                                         <classifier>features</classifier>
209                                     </artifact>
210                                 </artifacts>
211                             </configuration>
212                         </execution>
213                     </executions>
214                 </plugin>
215                 <plugin>
216                     <groupId>org.apache.maven.plugins</groupId>
217                     <artifactId>maven-resources-plugin</artifactId>
218                     <executions>
219                         <execution>
220                             <id>filter</id>
221                             <phase>generate-resources</phase>
222                             <goals>
223                                 <goal>resources</goal>
224                             </goals>
225                         </execution>
226                     </executions>
227                 </plugin>
228                 <plugin>
229                     <groupId>org.apache.maven.plugins</groupId>
230                     <artifactId>maven-surefire-plugin</artifactId>
231                     <configuration>
232                         <dependenciesToScan>
233                             <dependency>org.opendaylight.yangtools:features-test</dependency>
234                         </dependenciesToScan>
235                     </configuration>
236                 </plugin>
237             </plugins>
238         </pluginManagement>
239     </build>
240
241     <dependencies>
242         <!-- test the features.xml -->
243         <dependency>
244             <groupId>org.opendaylight.yangtools</groupId>
245             <artifactId>features-test</artifactId>
246             <version>0.7.0-SNAPSHOT</version>
247             <scope>test</scope>
248         </dependency>
249     </dependencies>
250 </project>