Skip to content
Browse files

new script to create a POM for a project

  • Loading branch information...
1 parent a1d414b commit d053932868dac123388176d2cca4386d6bde1398 @graemerocher graemerocher committed May 11, 2012
View
87 ...s-bootstrap/src/main/groovy/org/codehaus/groovy/grails/cli/maven/MavenPomGenerator.groovy
@@ -0,0 +1,87 @@
+/*
+ * Copyright 2012 SpringSource
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.codehaus.groovy.grails.cli.maven
+
+import org.codehaus.groovy.grails.cli.api.BaseSettingsApi
+import grails.util.BuildSettings
+import grails.util.Metadata
+import org.codehaus.groovy.grails.resolve.EnhancedDefaultDependencyDescriptor
+import org.apache.ivy.core.module.descriptor.DependencyDescriptor
+import org.codehaus.groovy.grails.resolve.IvyDependencyManager
+
+/**
+ * Generates a POM for a Grails application
+ *
+ * @author Graeme Rocher
+ * @since 2.1
+ */
+class MavenPomGenerator extends BaseSettingsApi{
+ MavenPomGenerator(BuildSettings buildSettings) {
+ super(buildSettings, false)
+ }
+
+ void generate(String group) {
+ def rootTemplate = grailsResource("src/grails/templates/maven/single.pom")
+
+ def baseDir = buildSettings.baseDir
+
+ final metadata = Metadata.getCurrent()
+ def name = metadata.getApplicationName()
+ def version = metadata.getApplicationVersion()
+
+ final pomFile = new File(baseDir, "pom.xml")
+ copyGrailsResource(pomFile,rootTemplate)
+
+ def dependencyManager = buildSettings.dependencyManager
+ List<String> dependencies = []
+ addDependenciesForScope(dependencyManager, "compile", dependencies)
+ addDependenciesForScope(dependencyManager, "runtime", dependencies)
+ addDependenciesForScope(dependencyManager, "test", dependencies)
+ addDependenciesForScope(dependencyManager, "provided", dependencies)
+ List<String> plugins = []
+ addDependenciesForScope(dependencyManager, "compile", plugins, "<type>zip</type>")
+ addDependenciesForScope(dependencyManager, "runtime", plugins, "<type>zip</type>")
+ addDependenciesForScope(dependencyManager, "test", plugins, "<type>zip</type>")
+ addDependenciesForScope(dependencyManager, "provided", plugins, "<type>zip</type>")
+
+
+ def ant = new AntBuilder()
+ ant.replace(file:pomFile) {
+ replacefilter token:"@grailsVersion@", value:buildSettings.grailsVersion
+ replacefilter token:"@group@", value:group
+ replacefilter token:"@name@", value:name
+ replacefilter token:"@version@", value:version
+ replacefilter token:"@dependencies@", value:dependencies.join(System.getProperty("line.separator"))
+ replacefilter token:"@plugins@", value:plugins.join(System.getProperty("line.separator"))
+
+ }
+ }
+
+ def addDependenciesForScope(IvyDependencyManager dependencyManager, String scope, ArrayList<String> dependencies, String type = "") {
+ final appDependencies = type ? dependencyManager.pluginDependencyDescriptors : dependencyManager.getApplicationDependencyDescriptors(scope)
+ dependencies.addAll(appDependencies.findAll { EnhancedDefaultDependencyDescriptor dd -> dd.scope == scope }.collect() { EnhancedDefaultDependencyDescriptor dd ->
+ """
+ <dependency>
+ <groupId>$dd.dependencyId.organisation</groupId>
+ <artifactId>$dd.dependencyId.name</artifactId>
+ <version>$dd.dependencyRevisionId.revision</version>
+ <scope>runtime</scope>
+ $type
+ </dependency>
+ """.toString()
+ })
+ }
+}
View
2 ...trap/src/main/groovy/org/codehaus/groovy/grails/resolve/AbstractIvyDependencyManager.java
@@ -283,7 +283,7 @@ public void setMetadataRegisteredPluginNames(Set<String> metadataRegisteredPlugi
/**
* Obtains a set of plugin dependency descriptors defined in the project
*/
- Set<DependencyDescriptor> getPluginDependencyDescriptors() {
+ public Set<DependencyDescriptor> getPluginDependencyDescriptors() {
return pluginDependencyDescriptors;
}
View
127 grails-resources/src/grails/templates/maven/single.pom
@@ -0,0 +1,127 @@
+<?xml version="1.0" encoding="utf-8"?>
+<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/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>@group@</groupId>
+ <artifactId>@name@</artifactId>
+ <packaging>grails-app</packaging>
+ <version>@version@</version>
+ <name>@name@</name>
+ <description>@version@</description>
+
+ <properties>
+ <grails.version>@grailsVersion@</grails.version>
+ </properties>
+
+ <dependencies>
+
+ <dependency>
+ <groupId>org.grails</groupId>
+ <artifactId>grails-dependencies</artifactId>
+ <version>${grails.version}</version>
+ <type>pom</type>
+ </dependency>
+
+ <dependency>
+ <groupId>org.grails</groupId>
+ <artifactId>grails-plugin-testing</artifactId>
+ <version>${grails.version}</version>
+ <scope>test</scope>
+ </dependency>
+
+ @dependencies@
+
+ @plugins@
+
+
+ </dependencies>
+
+ <build>
+ <pluginManagement />
+
+ <plugins>
+ <!-- Disables the Maven surefire plugin for Grails applications, as we have our own test runner -->
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <configuration>
+ <skip>true</skip>
+ </configuration>
+ <executions>
+ <execution>
+ <id>surefire-it</id>
+ <phase>integration-test</phase>
+ <goals>
+ <goal>test</goal>
+ </goals>
+ <configuration>
+ <skip>false</skip>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-clean-plugin</artifactId>
+ <version>2.4.1</version>
+ <configuration>
+ <filesets>
+ <fileset>
+ <directory>plugins</directory>
+ <includes>
+ <include>**/*</include>
+ </includes>
+ <followSymlinks>false</followSymlinks>
+ </fileset>
+ </filesets>
+ </configuration>
+ </plugin>
+
+ <plugin>
+ <groupId>org.grails</groupId>
+ <artifactId>grails-maven-plugin</artifactId>
+ <version>${grails.version}</version>
+ <configuration>
+ <!-- Whether for Fork a JVM to run Grails commands -->
+ <fork>true</fork>
+ </configuration>
+ <extensions>true</extensions>
+ </plugin>
+ </plugins>
+ </build>
+
+ <repositories>
+ <repository>
+ <id>grails</id>
+ <name>grails</name>
+ <url>http://repo.grails.org/grails/core</url>
+ </repository>
+ <repository>
+ <id>grails-plugins</id>
+ <name>grails-plugins</name>
+ <url>http://repo.grails.org/grails/plugins</url>
+ </repository>
+ </repositories>
+
+ <profiles>
+ <profile>
+ <id>tools</id>
+ <activation>
+ <property>
+ <name>java.vendor</name>
+ <value>Sun Microsystems Inc.</value>
+ </property>
+ </activation>
+ <dependencies>
+ <dependency>
+ <groupId>com.sun</groupId>
+ <artifactId>tools</artifactId>
+ <version>${java.version}</version>
+ <scope>system</scope>
+ <systemPath>${java.home}/../lib/tools.jar</systemPath>
+ </dependency>
+ </dependencies>
+ </profile>
+ </profiles>
+</project>
View
58 scripts/CreatePom.groovy
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2004-2005 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @author Graeme Rocher
+ * @since 2.1
+ */
+
+import org.codehaus.groovy.grails.cli.maven.MavenPomGenerator
+
+includeTargets << grailsScript("_GrailsArgParsing")
+target(default: "Creates a POM for a Grails project") {
+ depends(parseArguments)
+
+ if (!argsMap.params) {
+ println msg()
+ exit 1
+ }
+
+ def generator = new MavenPomGenerator(grailsSettings)
+ try {
+ def group = argsMap.params[0]?.trim()
+ if(group) {
+ generator.generate group
+ grailsConsole.addStatus "POM generated."
+ }
+ else {
+ println msg()
+ exit 1
+ }
+
+ }
+ catch(e) {
+ grailsConsole.error "Error occurred creating POM: ${e.message}", e
+ exit 1
+ }
+
+}
+
+String msg() {
+ return '''\
+Usage: grails create-pom <group>
+Example: grails create-pom com.mycompany
+'''
+}

0 comments on commit d053932

Please sign in to comment.
Something went wrong with that request. Please try again.