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