63 | *
64 | * @since 2.0
65 | */
66 | public class FailClosureTarget
67 | implements ClosureTarget
68 | {
69 | @VisibleForTesting
70 | static final String FAILED = "Failed";
71 |
72 | /**
73 | * Throws {@link MojoExecutionException}.
74 | */
75 | public Object call(final @Nullable Object[] args) throws Exception {
76 | if (args == null || args.length == 0) {
77 | throw new MojoExecutionException(FAILED);
78 | }
79 | else if (args.length == 1) {
80 | if (args[0] instanceof Throwable) {
81 | Throwable cause = (Throwable) args[0];
82 | throw new MojoExecutionException(cause.getMessage(), cause);
83 | }
84 | else {
85 | throw new MojoExecutionException(String.valueOf(args[0]));
86 | }
87 | }
88 | else if (args.length == 2) {
89 | if (args[1] instanceof Throwable) {
90 | throw new MojoExecutionException(String.valueOf(args[0]), (Throwable) args[1]);
91 | }
92 | else {
93 | throw new Error("Invalid arguments to fail(Object, Throwable), second argument must be a Throwable");
94 | }
95 | }
96 | else {
97 | throw new Error("Too many arguments; expected one of: fail(), fail(Object) or fail(Object, Throwable)");
98 | }
99 | }
100 | }
--------------------------------------------------------------------------------
/groovy-maven-plugin/src/main/java/org/codehaus/gmaven/plugin/GroovyRuntimeFactory.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2006-present the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.codehaus.gmaven.plugin;
17 |
18 | import java.util.Iterator;
19 | import java.util.ServiceLoader;
20 |
21 | import com.google.common.annotations.VisibleForTesting;
22 | import org.codehaus.gmaven.adapter.GroovyRuntime;
23 | import org.codehaus.plexus.component.annotations.Component;
24 | import org.slf4j.Logger;
25 | import org.slf4j.LoggerFactory;
26 |
27 | import static com.google.common.base.Preconditions.checkNotNull;
28 | import static com.google.common.base.Preconditions.checkState;
29 |
30 | /**
31 | * Creates {@link GroovyRuntime} instances.
32 | *
33 | * @since 2.0
34 | */
35 | @Component(role=GroovyRuntimeFactory.class)
36 | public class GroovyRuntimeFactory
37 | {
38 | private final Logger log = LoggerFactory.getLogger(getClass());
39 |
40 | public GroovyRuntime create(final ClassLoader classLoader) {
41 | checkNotNull(classLoader);
42 |
43 | Iterator services = findServices(classLoader);
44 |
45 | checkState(services.hasNext(), "No GroovyRuntime service found");
46 |
47 | GroovyRuntime runtime = services.next();
48 |
49 | checkState(!services.hasNext(), "Multiple GroovyRuntime services found");
50 |
51 | log.debug("Runtime: {}", runtime);
52 | return runtime;
53 | }
54 |
55 | @VisibleForTesting
56 | protected Iterator findServices(final ClassLoader classLoader) {
57 | ServiceLoader serviceLoader = ServiceLoader.load(GroovyRuntime.class, classLoader);
58 | log.debug("Loader: {}", serviceLoader);
59 | return serviceLoader.iterator();
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/groovy-maven-plugin/src/main/java/org/codehaus/gmaven/plugin/MojoResourceLoader.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2006-present the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.codehaus.gmaven.plugin;
17 |
18 | import java.io.File;
19 | import java.net.MalformedURLException;
20 | import java.net.URL;
21 | import java.util.List;
22 |
23 | import javax.annotation.Nullable;
24 |
25 | import org.codehaus.gmaven.adapter.ClassSource;
26 | import org.codehaus.gmaven.adapter.ResourceLoader;
27 |
28 | /**
29 | * Mojo {@link ResourceLoader}.
30 | *
31 | * @since 2.0
32 | */
33 | public class MojoResourceLoader
34 | extends BasicResourceLoader
35 | {
36 | private final ClassSource classSource;
37 |
38 | private final List scriptpath;
39 |
40 | public MojoResourceLoader(final ClassLoader classLoader,
41 | final @Nullable ClassSource classSource,
42 | final @Nullable List scriptpath)
43 | {
44 | super(classLoader);
45 | this.classSource = classSource;
46 | this.scriptpath = scriptpath;
47 | }
48 |
49 | public MojoResourceLoader(final ClassLoader classLoader,
50 | final @Nullable List scriptpath)
51 | {
52 | this(classLoader, null, scriptpath);
53 | }
54 |
55 | @Nullable
56 | protected URL resolve(final String className, final ClassLoader classLoader) throws MalformedURLException {
57 | String name = resourceName(className);
58 |
59 | // First check the scriptpath
60 | if (scriptpath != null) {
61 | for (File path : scriptpath) {
62 | File file = new File(path, name);
63 | if (file.exists()) {
64 | return file.toURI().toURL();
65 | }
66 | }
67 | }
68 |
69 | // Then look for a resource in the classpath
70 | URL url = classLoader.getResource(name);
71 |
72 | // FIXME: Sort out if this is required to function or if it should be removed
73 | // Try w/o leading '/'... ??? Seems that when loading resources the '/' prefix messes things up?
74 | if (url == null) {
75 | if (name.startsWith("/")) {
76 | String tmp = name.substring(1, name.length());
77 | url = classLoader.getResource(tmp);
78 | }
79 | }
80 |
81 | if (url != null) {
82 | return url;
83 | }
84 |
85 | // Check for a class defined in a file next to the main script file
86 | if (classSource != null) {
87 | File script = classSource.getFile();
88 | if (script != null) {
89 | File file = new File(script.getParentFile(), name);
90 |
91 | if (file.exists()) {
92 | return file.toURI().toURL();
93 | }
94 | }
95 | }
96 |
97 | // else fallback to super impl
98 | return super.resolve(className, classLoader);
99 | }
100 | }
--------------------------------------------------------------------------------
/groovy-maven-plugin/src/main/java/org/codehaus/gmaven/plugin/MojoSupport.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2006-present the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.codehaus.gmaven.plugin;
17 |
18 | import com.google.common.base.Throwables;
19 | import org.apache.maven.plugin.AbstractMojo;
20 | import org.apache.maven.plugin.Mojo;
21 | import org.apache.maven.plugin.MojoExecutionException;
22 | import org.apache.maven.plugin.MojoFailureException;
23 | import org.slf4j.Logger;
24 | import org.slf4j.LoggerFactory;
25 |
26 | /**
27 | * Support for {@link Mojo} implementations.
28 | *
29 | * @since 2.0
30 | */
31 | public abstract class MojoSupport
32 | extends AbstractMojo
33 | {
34 | protected final Logger log = LoggerFactory.getLogger(getClass());
35 |
36 | protected MojoSupport() {
37 | log.trace("Construct");
38 | }
39 |
40 | public void execute() throws MojoExecutionException, MojoFailureException {
41 | if (isSkipped()) {
42 | getLog().info("Skipping as requested by the user");
43 | return;
44 | }
45 | try {
46 | try {
47 | log.trace("Prepare");
48 | prepare();
49 |
50 | log.trace("Run");
51 | run();
52 | }
53 | finally {
54 | log.trace("Cleanup");
55 | cleanup();
56 | }
57 | }
58 | catch (Exception e) {
59 | log.trace("Failure", e);
60 | Throwables.propagateIfPossible(e, MojoExecutionException.class, MojoFailureException.class);
61 | throw Throwables.propagate(e);
62 | }
63 | }
64 |
65 | protected void prepare() throws Exception {
66 | // empty
67 | }
68 |
69 | protected abstract void run() throws Exception;
70 |
71 | protected void cleanup() throws Exception {
72 | // empty
73 | }
74 |
75 | protected boolean isSkipped() {
76 | return false;
77 | }
78 | }
79 |
--------------------------------------------------------------------------------
/groovy-maven-plugin/src/main/java/org/codehaus/gmaven/plugin/ShellMojo.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2006-present the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.codehaus.gmaven.plugin;
17 |
18 | import java.util.HashMap;
19 | import java.util.Map;
20 |
21 | import org.apache.maven.plugins.annotations.Mojo;
22 | import org.codehaus.gmaven.adapter.ResourceLoader;
23 | import org.codehaus.gmaven.adapter.ShellRunner;
24 |
25 | import static org.apache.maven.plugins.annotations.ResolutionScope.TEST;
26 |
27 | /**
28 | * Run {@code groovysh} shell.
29 | *
30 | *
31 | * See usage for more details.
32 | *
33 | * @since 2.0
34 | */
35 | @Mojo(name = "shell", requiresProject = false, requiresDependencyResolution = TEST, aggregator = true)
36 | public class ShellMojo
37 | extends RuntimeMojoSupport
38 | {
39 | // TODO: Expose groovysh options
40 |
41 | @Override
42 | protected void run() throws Exception {
43 | final ResourceLoader resourceLoader = new MojoResourceLoader(getRuntimeRealm(), getScriptpath());
44 | final Map context = createContext();
45 | final Map options = new HashMap();
46 | final ShellRunner shell = getRuntime().createShellRunner();
47 |
48 | shell.run(getRuntimeRealm(), resourceLoader, context, options);
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/groovy-maven-plugin/src/main/java/org/codehaus/gmaven/plugin/package-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2006-present the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | /**
17 | * GMaven plugin.
18 | *
19 | * @since 2.0
20 | */
21 | package org.codehaus.gmaven.plugin;
22 |
--------------------------------------------------------------------------------
/groovy-maven-plugin/src/main/java/org/codehaus/gmaven/plugin/util/ContainerHelper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2006-present the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.codehaus.gmaven.plugin.util;
17 |
18 | import com.google.common.annotations.VisibleForTesting;
19 | import org.codehaus.plexus.PlexusContainer;
20 | import org.codehaus.plexus.component.annotations.Component;
21 | import org.codehaus.plexus.component.annotations.Requirement;
22 | import org.slf4j.Logger;
23 | import org.slf4j.LoggerFactory;
24 |
25 | import static com.google.common.base.Preconditions.checkNotNull;
26 |
27 | /**
28 | * IoC container helper.
29 | *
30 | * @since 2.0
31 | */
32 | @SuppressWarnings("unchecked")
33 | @Component(role = ContainerHelper.class)
34 | public class ContainerHelper
35 | {
36 | private final Logger log = LoggerFactory.getLogger(getClass());
37 |
38 | @Requirement
39 | private PlexusContainer container;
40 |
41 | public ContainerHelper() {}
42 |
43 | @VisibleForTesting
44 | public ContainerHelper(final PlexusContainer container) {
45 | this.container = checkNotNull(container);
46 | }
47 |
48 | public Object lookup(final Class type) throws Exception {
49 | checkNotNull(type);
50 |
51 | log.trace("Lookup; class: {}", type);
52 | Object component = container.lookup(type);
53 | log.trace("Component: {}", component);
54 |
55 | return component;
56 | }
57 |
58 | public Object lookup(final String typeName) throws Exception {
59 | checkNotNull(typeName);
60 |
61 | log.trace("Lookup; class-name: {}", typeName);
62 | Object component = container.lookup(typeName);
63 | log.trace("Component: {}", component);
64 |
65 | return component;
66 | }
67 |
68 | public Object lookup(final Class type, final String name) throws Exception {
69 | checkNotNull(type);
70 | checkNotNull(name);
71 |
72 | log.trace("Lookup; class: {}, name: {}", type, name);
73 | Object component = container.lookup(type, name);
74 | log.trace("Component: {}", component);
75 |
76 | return component;
77 | }
78 |
79 | public Object lookup(final String typeName, final String name) throws Exception {
80 | checkNotNull(typeName);
81 | checkNotNull(name);
82 |
83 | log.trace("Lookup; class-name: {}, name: {}", typeName, name);
84 | Object component = container.lookup(typeName, name);
85 | log.trace("Component: {}", component);
86 |
87 | return component;
88 | }
89 | }
90 |
--------------------------------------------------------------------------------
/groovy-maven-plugin/src/main/java/org/codehaus/gmaven/plugin/util/GroovyVersionHelper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2006-present the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.codehaus.gmaven.plugin.util;
17 |
18 | import java.lang.reflect.Method;
19 |
20 | import javax.annotation.Nullable;
21 |
22 | import com.google.common.annotations.VisibleForTesting;
23 | import org.codehaus.plexus.component.annotations.Component;
24 | import org.codehaus.plexus.component.annotations.Requirement;
25 | import org.eclipse.aether.version.Version;
26 | import org.slf4j.Logger;
27 | import org.slf4j.LoggerFactory;
28 |
29 | import static com.google.common.base.Preconditions.checkNotNull;
30 |
31 | /**
32 | * Groovy version helper.
33 | *
34 | * @since 2.0
35 | */
36 | @Component(role=GroovyVersionHelper.class)
37 | public class GroovyVersionHelper
38 | {
39 | private final Logger log = LoggerFactory.getLogger(getClass());
40 |
41 | @Requirement
42 | private VersionHelper versionHelper;
43 |
44 | public GroovyVersionHelper() {}
45 |
46 | @VisibleForTesting
47 | public GroovyVersionHelper(final VersionHelper versionHelper) {
48 | this.versionHelper = checkNotNull(versionHelper);
49 | }
50 |
51 | @Nullable
52 | public Version detectVersion(final ClassLoader classLoader) {
53 | checkNotNull(classLoader);
54 |
55 | log.trace("Detecting Groovy version; class-loader: {}", classLoader);
56 |
57 | // Modern versions of Groovy expose the version via GroovySystem.getVersion()
58 | String raw = getVersion(classLoader, "groovy.lang.GroovySystem", "getVersion");
59 | if (raw == null) {
60 | // Older versions of Groovy expose the version via InvokerHelper.getVersion()
61 | raw = getVersion(classLoader, "org.codehaus.groovy.runtime.InvokerHelper", "getVersion");
62 | }
63 |
64 | log.trace("Raw version: {}", raw);
65 | if (raw == null) {
66 | return null;
67 | }
68 |
69 | Version version = versionHelper.parseVersion(raw);
70 | log.trace("Version: {}", version);
71 | return version;
72 | }
73 |
74 | /**
75 | * Get version from Groovy version helper utilities via reflection.
76 | */
77 | private String getVersion(final ClassLoader classLoader, final String className, final String methodName) {
78 | log.trace("Getting version; class-loader: {}, class-name: {}, method-name: {}",
79 | classLoader, className, methodName);
80 |
81 | try {
82 | Class> type = classLoader.loadClass(className);
83 | Method method = type.getMethod(methodName);
84 | Object result = method.invoke(null);
85 | if (result != null) {
86 | return result.toString();
87 | }
88 | }
89 | catch (Throwable e) {
90 | log.trace("Unable determine version from: {}", className);
91 | }
92 |
93 | return null;
94 | }
95 | }
96 |
--------------------------------------------------------------------------------
/groovy-maven-plugin/src/main/java/org/codehaus/gmaven/plugin/util/Maps2.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2006-present the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.codehaus.gmaven.plugin.util;
17 |
18 | import java.util.Enumeration;
19 | import java.util.HashMap;
20 | import java.util.Map;
21 | import java.util.Properties;
22 |
23 | import static com.google.common.base.Preconditions.checkNotNull;
24 |
25 | /**
26 | * Map helper.
27 | *
28 | * @since 2.1.1
29 | */
30 | public class Maps2
31 | {
32 | private Maps2() {
33 | // empty
34 | }
35 |
36 | public static Map fromProperties(final Properties properties) {
37 | checkNotNull(properties);
38 | Map result = new HashMap(properties.size());
39 | Enumeration names = properties.propertyNames();
40 | while (names.hasMoreElements()) {
41 | String name = (String)names.nextElement();
42 | result.put(name, properties.getProperty(name));
43 | }
44 | return result;
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/groovy-maven-plugin/src/main/java/org/codehaus/gmaven/plugin/util/MavenVersionHelper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2006-present the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.codehaus.gmaven.plugin.util;
17 |
18 | import java.io.IOException;
19 | import java.io.InputStream;
20 | import java.net.URL;
21 | import java.util.Properties;
22 |
23 | import javax.annotation.Nullable;
24 |
25 | import com.google.common.annotations.VisibleForTesting;
26 | import org.codehaus.plexus.component.annotations.Component;
27 | import org.codehaus.plexus.component.annotations.Requirement;
28 | import org.eclipse.aether.version.Version;
29 | import org.slf4j.Logger;
30 | import org.slf4j.LoggerFactory;
31 |
32 | import static com.google.common.base.Preconditions.checkNotNull;
33 |
34 | /**
35 | * Maven version helper.
36 | *
37 | * @since 2.0
38 | */
39 | @Component(role = MavenVersionHelper.class)
40 | public class MavenVersionHelper
41 | {
42 | private final Logger log = LoggerFactory.getLogger(getClass());
43 |
44 | @Requirement
45 | private VersionHelper versionHelper;
46 |
47 | public MavenVersionHelper() {}
48 |
49 | @VisibleForTesting
50 | public MavenVersionHelper(final VersionHelper versionHelper) {
51 | this.versionHelper = checkNotNull(versionHelper);
52 | }
53 |
54 | @Nullable
55 | public Version detectVersion(final ClassLoader classLoader) {
56 | checkNotNull(classLoader);
57 |
58 | log.trace("Detecting Maven version; class-loader: {}", classLoader);
59 |
60 | // TODO: Sort out how compatible this logic is for newer/older versions of maven
61 |
62 | Properties props = readProperties(
63 | classLoader, "org.apache.maven.cli.MavenCli", "/org/apache/maven/messages/build.properties");
64 | String raw = props.getProperty("version");
65 |
66 | log.trace("Raw version: {}", raw);
67 | if (raw == null) {
68 | return null;
69 | }
70 |
71 | Version version = versionHelper.parseVersion(raw);
72 | log.trace("Version: {}", version);
73 | return version;
74 | }
75 |
76 | private Properties readProperties(final ClassLoader classLoader, final String className, final String resourceName) {
77 | log.trace("Reading properties; class-loader: {}, class-name: {}, resource-name: {}",
78 | classLoader, className, resourceName);
79 |
80 | Properties props = new Properties();
81 | try {
82 | Class type = classLoader.loadClass(className);
83 | log.trace("Type: {}", type);
84 |
85 | URL resource = type.getResource(resourceName);
86 | log.trace("Resource: {}", resource);
87 |
88 | if (resource != null) {
89 | InputStream stream = resource.openStream();
90 | try {
91 | props.load(stream);
92 | }
93 | finally {
94 | stream.close();
95 | }
96 | }
97 | }
98 | catch (ClassNotFoundException e) {
99 | log.trace("Failed to resolve class", e);
100 | }
101 | catch (IOException e) {
102 | log.trace("Failed to read properties", e);
103 | }
104 |
105 | return props;
106 | }
107 | }
108 |
--------------------------------------------------------------------------------
/groovy-maven-plugin/src/main/java/org/codehaus/gmaven/plugin/util/PropertiesBuilder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2006-present the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.codehaus.gmaven.plugin.util;
17 |
18 | import java.util.ArrayList;
19 | import java.util.Collections;
20 | import java.util.HashMap;
21 | import java.util.List;
22 | import java.util.Map;
23 | import java.util.Map.Entry;
24 |
25 | import javax.annotation.Nullable;
26 |
27 | import org.apache.maven.execution.MavenSession;
28 | import org.apache.maven.project.MavenProject;
29 | import org.codehaus.plexus.component.annotations.Component;
30 | import org.codehaus.plexus.interpolation.InterpolationException;
31 | import org.codehaus.plexus.interpolation.Interpolator;
32 | import org.codehaus.plexus.interpolation.MapBasedValueSource;
33 | import org.codehaus.plexus.interpolation.StringSearchInterpolator;
34 | import org.slf4j.Logger;
35 | import org.slf4j.LoggerFactory;
36 |
37 | /**
38 | * Helper to build merged Maven execution properties.
39 | *
40 | * @since 2.0
41 | */
42 | @Component(role = PropertiesBuilder.class, instantiationStrategy="per-lookup")
43 | public class PropertiesBuilder
44 | {
45 | private final Logger log = LoggerFactory.getLogger(getClass());
46 |
47 | private MavenProject project;
48 |
49 | private MavenSession session;
50 |
51 | private Map properties;
52 |
53 | private Map defaults;
54 |
55 | public PropertiesBuilder setProject(final @Nullable MavenProject project) {
56 | this.project = project;
57 | return this;
58 | }
59 |
60 | public PropertiesBuilder setSession(final @Nullable MavenSession session) {
61 | this.session = session;
62 | return this;
63 | }
64 |
65 | public PropertiesBuilder setProperties(final @Nullable Map properties) {
66 | this.properties = properties;
67 | return this;
68 | }
69 |
70 | public PropertiesBuilder setDefaults(final @Nullable Map defaults) {
71 | this.defaults = defaults;
72 | return this;
73 | }
74 |
75 | public Map build() {
76 | Map props = new HashMap();
77 | if (defaults != null) {
78 | props.putAll(defaults);
79 | }
80 | if (project != null) {
81 | props.putAll(Maps2.fromProperties(project.getProperties()));
82 | }
83 | if (session != null) {
84 | props.putAll(Maps2.fromProperties(session.getSystemProperties()));
85 | props.putAll(Maps2.fromProperties(session.getUserProperties()));
86 | }
87 | if (properties != null) {
88 | props.putAll(properties);
89 | }
90 |
91 | // resolve any dangling references which could exist due to custom properties/defaults
92 | props = resolve(props);
93 |
94 | if (log.isTraceEnabled()) {
95 | log.trace("Properties:");
96 | List keys = new ArrayList(props.keySet());
97 | Collections.sort(keys);
98 | for (String key : keys) {
99 | log.trace(" {}={}", key, props.get(key));
100 | }
101 | }
102 |
103 | return props;
104 | }
105 |
106 | private Map resolve(final Map source) {
107 | Map result = new HashMap(source.size());
108 | Interpolator interpolator = new StringSearchInterpolator();
109 | interpolator.addValueSource(new MapBasedValueSource(source));
110 |
111 | for (Entry entry : source.entrySet()) {
112 | String key = entry.getKey();
113 | String value = entry.getValue();
114 |
115 | if (value == null) {
116 | log.warn("Ignoring null property: {}", key);
117 | continue;
118 | }
119 |
120 | // skip unless value contains an expression
121 | if (value.contains(StringSearchInterpolator.DEFAULT_START_EXPR)) {
122 | try {
123 | value = interpolator.interpolate(value);
124 | log.trace("Resolved: {} -> '{}'", key, value);
125 | }
126 | catch (InterpolationException e) {
127 | log.warn("Failed to interpolate: {}, using original value: {}", key, value);
128 | }
129 | }
130 | result.put(key, value);
131 | }
132 |
133 | return result;
134 | }
135 | }
136 |
--------------------------------------------------------------------------------
/groovy-maven-plugin/src/main/java/org/codehaus/gmaven/plugin/util/VersionHelper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2006-present the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.codehaus.gmaven.plugin.util;
17 |
18 | import com.google.common.annotations.VisibleForTesting;
19 | import com.google.common.base.Throwables;
20 | import org.codehaus.plexus.component.annotations.Component;
21 | import org.eclipse.aether.util.version.GenericVersionScheme;
22 | import org.eclipse.aether.version.InvalidVersionSpecificationException;
23 | import org.eclipse.aether.version.Version;
24 | import org.eclipse.aether.version.VersionRange;
25 | import org.eclipse.aether.version.VersionScheme;
26 | import org.slf4j.Logger;
27 | import org.slf4j.LoggerFactory;
28 |
29 | import static com.google.common.base.Preconditions.checkArgument;
30 | import static com.google.common.base.Preconditions.checkNotNull;
31 |
32 | /**
33 | * Aether version/range/scheme helpers.
34 | *
35 | * @since 2.0
36 | */
37 | @Component(role = VersionHelper.class)
38 | public class VersionHelper
39 | {
40 | private final Logger log = LoggerFactory.getLogger(getClass());
41 |
42 | /**
43 | * Earliest suffix to help Aether detect ranges _better_ when SNAPSHOT versions are used.
44 | */
45 | public static final String EARLIEST_SUFFIX = "alpha-SNAPSHOT";
46 |
47 | private final VersionScheme versionScheme;
48 |
49 | public VersionHelper() {
50 | this(new GenericVersionScheme());
51 | }
52 |
53 | @VisibleForTesting
54 | public VersionHelper(final VersionScheme versionScheme) {
55 | this.versionScheme = checkNotNull(versionScheme);
56 | }
57 |
58 | public VersionScheme getScheme() {
59 | return versionScheme;
60 | }
61 |
62 | /**
63 | * Parses a {@link Version} and propagates exceptions.
64 | */
65 | public Version parseVersion(final String version) {
66 | checkNotNull(version);
67 | try {
68 | return versionScheme.parseVersion(version);
69 | }
70 | catch (InvalidVersionSpecificationException e) {
71 | throw Throwables.propagate(e);
72 | }
73 | }
74 |
75 | /**
76 | * Parses a {@link VersionRange} and propagates exceptions.
77 | */
78 | public VersionRange parseRange(final String pattern) {
79 | checkNotNull(pattern);
80 | try {
81 | return versionScheme.parseVersionRange(pattern);
82 | }
83 | catch (InvalidVersionSpecificationException e) {
84 | throw Throwables.propagate(e);
85 | }
86 | }
87 |
88 | /**
89 | * Builds a range between version parts and parts[n] + 1.
90 | *
91 | * ie. range(1,2,3) produces a range between versions 1.2.3 and 1.2.4.
92 | */
93 | public VersionRange range(final int... parts) {
94 | checkNotNull(parts);
95 | checkArgument(parts.length != 0);
96 |
97 | StringBuilder buff = new StringBuilder()
98 | .append("[");
99 |
100 | for (int i = 0; i < parts.length; i++) {
101 | buff.append(parts[i]);
102 | if (i + 1 < parts.length) {
103 | buff.append(".");
104 | }
105 | }
106 |
107 | buff.append(",");
108 |
109 | for (int i = 0; i < parts.length; i++) {
110 | if (i + 1 == parts.length) {
111 | buff.append(parts[i] + 1);
112 | }
113 | else {
114 | buff.append(parts[i])
115 | .append(".");
116 | }
117 | }
118 |
119 | buff.append(")");
120 |
121 | log.trace("Range pattern: {}", buff);
122 |
123 | return parseRange(buff.toString());
124 | }
125 |
126 | /**
127 | * Builds a range before version parts.
128 | */
129 | public VersionRange before(final int... parts) {
130 | checkNotNull(parts);
131 | checkArgument(parts.length != 0);
132 |
133 | StringBuilder buff = new StringBuilder()
134 | .append("(,");
135 |
136 | for (int i = 0; i < parts.length; i++) {
137 | buff.append(parts[i]);
138 | if (i + 1 < parts.length) {
139 | buff.append(".");
140 | }
141 | }
142 |
143 | buff.append("-")
144 | .append(EARLIEST_SUFFIX)
145 | .append(")");
146 |
147 | log.trace("Range pattern: {}", buff);
148 |
149 | return parseRange(buff.toString());
150 | }
151 | }
152 |
--------------------------------------------------------------------------------
/groovy-maven-plugin/src/main/java/org/codehaus/gmaven/plugin/util/package-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2006-present the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | /**
17 | * GMaven plugin utilities.
18 | *
19 | * @since 2.0
20 | */
21 | package org.codehaus.gmaven.plugin.util;
22 |
--------------------------------------------------------------------------------
/groovy-maven-plugin/src/main/resources/META-INF/org.sonatype.gossip/config.properties:
--------------------------------------------------------------------------------
1 | #
2 | # Copyright (c) 2006-present the original author or authors.
3 | #
4 | # Licensed under the Apache License, Version 2.0 (the "License");
5 | # you may not use this file except in compliance with the License.
6 | # You may obtain a copy of the License at
7 | #
8 | # http://www.apache.org/licenses/LICENSE-2.0
9 | #
10 | # Unless required by applicable law or agreed to in writing, software
11 | # distributed under the License is distributed on an "AS IS" BASIS,
12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | # See the License for the specific language governing permissions and
14 | # limitations under the License.
15 | #
16 |
17 | version=1.0.0
18 |
19 | profiles=default, gmaven-common, gmaven-logging-debug, gmaven-logging-trace
20 |
21 | ##
22 | ## default
23 | ##
24 |
25 | profile.default.includes=gmaven-common
26 | profile.default.listeners=console
27 | profile.default.listener.console=org.sonatype.gossip.listener.ConsoleListener
28 | profile.default.listener.console.renderer=org.sonatype.gossip.render.PatternRenderer
29 | profile.default.listener.console.renderer.pattern=[%l] %m%n%x
30 | profile.default.logger.*=INFO
31 |
32 | ##
33 | ## Common
34 | ##
35 |
36 | profile.gmaven-common.listeners=console
37 | profile.gmaven-common.listener.console=org.sonatype.gossip.listener.ConsoleListener
38 | profile.gmaven-common.listener.console.renderer=org.sonatype.gossip.render.PatternRenderer
39 | profile.gmaven-common.listener.console.renderer.pattern=[%l] %c - %m%n%x
40 |
41 | ##
42 | ## gmaven.logging=DEBUG
43 | ##
44 |
45 | profile.gmaven-logging-debug.includes=gmaven-common
46 | profile.gmaven-logging-debug.triggers=default
47 | profile.gmaven-logging-debug.trigger.default=org.sonatype.gossip.trigger.SystemPropertyTrigger
48 | profile.gmaven-logging-debug.trigger.default.name=gmaven.logging
49 | profile.gmaven-logging-debug.trigger.default.value=DEBUG
50 | profile.gmaven-logging-debug.trigger.default.ignoreCase=true
51 | profile.gmaven-logging-debug.logger.*=DEBUG
52 |
53 | ##
54 | ## gmaven.logging=TRACE
55 | ##
56 |
57 | profile.gmaven-logging-trace.includes=gmaven-common
58 | profile.gmaven-logging-trace.triggers=default
59 | profile.gmaven-logging-trace.trigger.default=org.sonatype.gossip.trigger.SystemPropertyTrigger
60 | profile.gmaven-logging-trace.trigger.default.name=gmaven.logging
61 | profile.gmaven-logging-trace.trigger.default.value=TRACE
62 | profile.gmaven-logging-trace.trigger.default.ignoreCase=true
63 | profile.gmaven-logging-trace.logger.*=TRACE
--------------------------------------------------------------------------------
/groovy-maven-plugin/src/site/markdown/classpath.md:
--------------------------------------------------------------------------------
1 |
18 | # Classpath
19 |
20 | When executing a goal with a Maven project, GMaven can provide access to the classpath of the project for a
21 | particular scope. This is controlled by the `classpathScope` parameter.
22 |
23 | For example, to execute a script and include the `runtime` scope dependencencies on the classpath:
24 |
25 |
26 | org.codehaus.gmaven
27 | groovy-maven-plugin
28 |
29 |
30 | process-classes
31 |
32 | execute
33 |
34 |
35 | runtime
36 |
37 | // this script has access to the compiled classes and all depencencies of scope=runtime.
38 |
39 |
40 |
41 |
42 |
43 |
44 | By default `classpathScope` is set to `none` which means that no additional classpath elements will be available.
45 |
46 | When executing a goal directly without a project, the scope may be configured by setting the `scope` property:
47 |
48 | mvn groovy:shell -Dscope=test
49 |
50 | For all supported values and additional documentation see
51 | [ClasspathScope](apidocs/org/codehaus/gmaven/plugin/ClasspathScope.html).
52 |
53 | Any additional classpath which may be needed can also be configured on the plugin definition:
54 |
55 |
56 | org.codehaus.gmaven
57 | groovy-maven-plugin
58 |
59 |
60 | commons-lang
61 | commons-lang
62 | 2.6
63 |
64 |
65 |
66 |
--------------------------------------------------------------------------------
/groovy-maven-plugin/src/site/markdown/console.md:
--------------------------------------------------------------------------------
1 |
18 | # Console
19 |
20 | GMaven supports opening up the GUI [Groovy Console](http://groovy.codehaus.org/Groovy+Console)
21 | with the [console](console-mojo.html) goal:
22 |
23 | mvn groovy:console
24 |
25 | This goal works with and without a project. When a project is available additional [classpath](classpath.html)
26 | configuration options are avaiable.
27 |
28 | All context [variables](variables.html) are availble for use in the console.
29 |
30 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/groovy-maven-plugin/src/site/markdown/execute.md:
--------------------------------------------------------------------------------
1 |
18 | # Execute
19 |
20 | Groovy script execution is the main purpose of GMaven and is available with the [execute](execute-mojo.html) goal.
21 | A script can be executed as part of a build (attached to a lifecycle phase) or directly via the command-line.
22 |
23 | This goal is not bound to a [lifecycle phase](http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_Reference)
24 | by default, so the phase must always be configured, and the phase will depend highly on what the purpose is that you are
25 | executing a script for.
26 |
27 | This goal works with and without a project. When a project is available additional [classpath](classpath.html)
28 | configuration options are avaiable.
29 |
30 | All context [variables](variables.html) are availble for use in executed scripts.
31 |
32 | ## Source
33 |
34 | The configuration of the _source_ of the script to execute can be configured to be one of:
35 |
36 | * File
37 | * URL
38 | * Inline
39 |
40 | This value can be specified by the `source` configuration parameter or by the `source` property.
41 |
42 | ### File
43 |
44 | Files can be used, and for most complex usage recommend, as the source of the script to execute:
45 |
46 |
47 | org.codehaus.gmaven
48 | groovy-maven-plugin
49 |
50 |
51 | generate-resources
52 |
53 | execute
54 |
55 |
56 | ${project.basedir}/src/main/script/myscript.groovy
57 |
58 |
59 |
60 |
61 |
62 | or:
63 |
64 | mvn groovy:execute -Dsource=src/main/script/myscript.groovy
65 |
66 | This form only works when the file actually _exists_ and if the file is missing it could cause
67 | GMaven to treat the source as _inline_ instead.
68 |
69 | ### URL
70 |
71 | URL sources allow the script to be resolved by a URL, which can be any URL which the JVM can resolve.
72 |
73 |
74 | org.codehaus.gmaven
75 | groovy-maven-plugin
76 |
77 |
78 | generate-resources
79 |
80 | execute
81 |
82 |
83 | http://mydomain.com/myscript.groovy
84 |
85 |
86 |
87 |
88 |
89 | or:
90 |
91 | mvn groovy:execute -Dsource=http://mydomain.com/myscript.groovy
92 |
93 | ### Inline
94 |
95 | Inline allows scripts to be defined inside of project files.
96 |
97 |
98 | org.codehaus.gmaven
99 | groovy-maven-plugin
100 |
101 |
102 | generate-resources
103 |
104 | execute
105 |
106 |
107 |
108 | println 'Hello'
109 |
110 |
111 |
112 |
113 |
114 |
115 | or:
116 |
117 | mvn groovy:execute -Dsource="println 'Hello'"
118 |
119 | #### Maven interpolation and GStrings
120 |
121 | While this form may be the simplest to use for many cases, there are some pitfalls to be aware of. Specifically
122 | use of [GStrings](http://groovy.codehaus.org/Strings+and+GString) (special String-like constructs available to Groovy),
123 | can cause some unexpected behavior due to Maven interpolation syntax `${}` overloading the GString syntax.
124 |
125 | When Maven loads a project, it very early on, interpolates the content of the POM to replace properties and simple
126 | expressions with string values. This is very common and very handy, but it does provide some complication when
127 | Maven attempts to resolve an expression which was expected to be resolved by a GString.
128 |
129 | Scripts which make heavy use of GStrings should seriously consider using a File-based source configuration to
130 | avoid this potential complication.
131 |
132 | ## Properties and Defaults
133 |
134 | Additional customization of the execution variable `properties` are available for scripts run with `execute`.
135 |
136 | Script executions can have _overriding_ properties by setting the `properties` configuration parameter.
137 | Values set here will take precendence over any other property definition (user, system, project, defaults, etc).
138 |
139 |
140 | org.codehaus.gmaven
141 | groovy-maven-plugin
142 |
143 |
144 | generate-resources
145 |
146 | execute
147 |
148 |
149 |
150 | Xenu
151 |
152 |
153 | println 'Hello ' + properties['name']
154 |
155 |
156 |
157 |
158 |
159 |
160 | This example will always print `Hello Xenu` even if `mvn -Dname=Steve` or if the project
161 | defined a property of the same name.
162 |
163 | Script executions can also have _default_ properties by setting the `defaults` configuration parameter.
164 | Values set here will only be used if no other property for the same nme definition exists.
165 |
166 |
167 | org.codehaus.gmaven
168 | groovy-maven-plugin
169 |
170 |
171 | generate-resources
172 |
173 | execute
174 |
175 |
176 |
177 | Xenu
178 |
179 |
180 | println 'Hello ' + properties['name']
181 |
182 |
183 |
184 |
185 |
186 |
187 | Here, with out any other definition of `name`, will print `Hello Xenu`, but if instead was invoked with
188 | `mvn -Dname=Jason` would print `Hello Jason` instead.
189 |
--------------------------------------------------------------------------------
/groovy-maven-plugin/src/site/markdown/index.md:
--------------------------------------------------------------------------------
1 |
18 | # Groovy Maven Plugin
19 |
20 | See _USAGE_ content for more details.
21 |
22 | ### Hard Requirements
23 |
24 | The Maven and Groovy version requirements are ***hard*** requirements.
25 |
26 | When goals are executing the versions of Maven and Groovy are detected.
27 | If they are not compatible the goals will fail with an error.
28 |
29 | ### Customizing Groovy Version
30 |
31 | To customize the version of Groovy the plugin will use, override the `org.codehaus.groovy:groovy-all` dependency
32 | on the plugin definition in the project.
33 |
34 | For example to use Groovy 2.0.6 instead of the default:
35 |
36 |
37 | org.codehaus.gmaven
38 | groovy-maven-plugin
39 |
40 |
41 | org.codehaus.groovy
42 | groovy-all
43 | 2.0.6
44 |
45 |
46 |
47 |
48 | There is currently no way to change the Groovy version without a project.
49 | Direct execution of goals will always use the default Groovy version if no project is available. When a project
50 | is available, then `pluginManagement` can be used to configure direct goal execution.
51 |
--------------------------------------------------------------------------------
/groovy-maven-plugin/src/site/markdown/logging.md:
--------------------------------------------------------------------------------
1 |
18 | # Logging
19 |
20 | ## Apache Maven 3.0+
21 |
22 | GMaven uses SLF4J and uses the [Gossip](https://github.com/jdillon/gossip) provider to allow for simple and
23 | flexible configuration of logging output.
24 |
25 | When running in Apache Maven 3.0.x without any SLF4J customizations, additional GMaven logging can be enabled
26 | by setting the `gmaven.logging` property on the `mvn` command-line.
27 |
28 | To enable `DEBUG` logging:
29 |
30 | mvn -Dgmaven.logging=DEBUG
31 |
32 | To enable `TRACE` logging:
33 |
34 | mvn -Dgmaven.logging=TRACE
35 |
36 | ## Apache Maven 3.1+
37 |
38 | Apache Maven 3.1.x provides SLF4J bindings [slf4j-simple](http://www.slf4j.org/apidocs/org/slf4j/impl/SimpleLogger.html)
39 | by default and that binding implementation must be configured to enable more verbose logging details.
40 |
41 | To enable logging configuration as described for Apache Maven 3.0+ the 3.1+ distribution needs to be
42 | augmented to replace the default SLF4J binding with Gossip.
43 |
--------------------------------------------------------------------------------
/groovy-maven-plugin/src/site/markdown/scriptpath.md:
--------------------------------------------------------------------------------
1 |
18 | # Scriptpath
19 |
20 | The `scriptpath` is a path where GMaven will search for additional Groovy sources to resolve when executing a Groovy script.
21 |
22 | This configuration applies to all goals. This means that you can access your external script via the `console`
23 | and `shell` goals as well as `execute`.
24 |
25 | ### Explicit
26 |
27 | For example if you have a class named `Helper` defined in a file named `${project.basedir}/src/main/script/Helper.groovy`
28 | your scripts can use that class if you configure the script path to include `${project.basedir}/src/main/script`:
29 |
30 |
31 | org.codehaus.gmaven
32 | groovy-maven-plugin
33 |
34 |
35 | generate-resources
36 |
37 | execute
38 |
39 |
40 |
41 | ${project.basedir}/src/main/script
42 |
43 |
44 | import Helper
45 | def h = new Helper()
46 |
47 |
48 |
49 |
50 |
51 |
52 | ### Peer
53 |
54 | Additionally if using a file-based source configuration, GMaven will automatically look for
55 | matching classes _nexus to_ the source file.
56 |
57 | So if you had a `${project.basedir}/src/main/script/Main.groovy` with something like:
58 |
59 | import Helper
60 | def h = new Helper()
61 |
62 | ... then this configuration would be sufficent to allow the source file script to access to
63 | `${project.basedir}/src/main/script/Helper.groovy` script:
64 |
65 |
66 | org.codehaus.gmaven
67 | groovy-maven-plugin
68 |
69 |
70 | generate-resources
71 |
72 | execute
73 |
74 |
75 | ${project.basedir}/src/main/script/Main.groovy
76 |
77 |
78 |
79 |
80 |
81 | ### Command-line
82 |
83 | Configure the `scriptpath` property on the mvn command-line with a _comma seperated_ list of entries:
84 |
85 | mvn -Dscriptpath=dir1,dir2,dir3
86 |
--------------------------------------------------------------------------------
/groovy-maven-plugin/src/site/markdown/shell.md:
--------------------------------------------------------------------------------
1 |
18 | # Shell
19 |
20 | GMaven supports running the command-line [Groovy Shell](http://groovy.codehaus.org/Groovy+Shell)
21 | (aka `groovysh`) with the [shell](shell-mojo.html) goal:
22 |
23 | mvn groovy:shell
24 |
25 | This goal works with and without a project. When a project is available additional [classpath](classpath.html)
26 | configuration options are avaiable.
27 |
28 | All context [variables](variables.html) are availble for use in the shell.
29 |
--------------------------------------------------------------------------------
/groovy-maven-plugin/src/site/markdown/variables.md.vm:
--------------------------------------------------------------------------------
1 |
Variables
2 |
3 | All goals which invoke the Groovy runtime, expose a set of predefined context (aka binding) variables.
4 |
5 |
Basic
6 |
7 | Basic context variables.
8 |
9 | | Name | Description
10 | | ---------- | -----------
11 | | project | The currently executing [MavenProject][MavenProject] or if no project, this will be a reference to the generic stub project.
12 | | basedir | [File][File] reference to detected base directory for Maven execution.
13 | | properties | [Properties][Properties] containing merged execution properties.
14 | | ant | Preconfigured [AntBuilder][AntBuilder].
15 | | fail | Closure to help fail execution. See [FailClosureTarget][FailClosureTarget] for supported syntax and usage.
16 | | log | SLF4J [Logger][Logger].
17 |
18 |
Advanced
19 |
20 | Advanced context variables.
21 |
22 | | Name | Description
23 | | ------------- | -----------
24 | | container | [ContainerHelper][ContainerHelper] to simplify looking up Maven components.
25 | | plugin | `groovy-maven-plugin` [PluginDescriptor][PluginDescriptor].
26 | | pluginContext | Plugin context [Map][Map] to allow communication between plugin goals. See [ContextEnabled][ContextEnabled] for more details.
27 | | mojo | The current [MojoExecution][MojoExecution].
28 | | session | The current [MavenSession][MavenSession].
29 | | settings | The current [Settings][Settings].
30 |
31 | [File]: ${site_javaApidocsUrl}/java/io/File.html
32 | [Properties]: ${site_javaApidocsUrl}/java/util/Properties.html
33 | [Map]: ${site_javaApidocsUrl}/java/util/Map.html
34 | [FailClosureTarget]: apidocs/org/codehaus/gmaven/plugin/FailClosureTarget.html
35 | [ContainerHelper]: apidocs/org/codehaus/gmaven/plugin/util/ContainerHelper.html
36 | [MavenProject]: ${site_mavenApidocsUrl}/org/apache/maven/project/MavenProject.html
37 | [AntBuilder]: ${site_groovyApidocsUrl}/groovy/util/AntBuilder.html
38 | [PluginDescriptor]: ${site_mavenApidocsUrl}/org/apache/maven/plugin/descriptor/PluginDescriptor.html
39 | [ContextEnabled]: ${site_mavenApidocsUrl}/org/apache/maven/plugin/ContextEnabled.html
40 | [MojoExecution]: ${site_mavenApidocsUrl}/org/apache/maven/plugin/MojoExecution.html
41 | [MavenSession]: ${site_mavenApidocsUrl}/org/apache/maven/execution/MavenSession.html
42 | [Settings]: ${site_mavenApidocsUrl}/org/apache/maven/settings/Settings.html
43 | [Logger]: http://www.slf4j.org/apidocs/org/slf4j/Logger.html
--------------------------------------------------------------------------------
/groovy-maven-plugin/src/site/site.xml:
--------------------------------------------------------------------------------
1 |
2 |
19 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
31 |
32 |
41 |
42 |
47 |
48 |
52 |
53 |
60 |
61 |
62 |
--------------------------------------------------------------------------------
/groovy-maven-plugin/src/test/java/org/codehaus/gmaven/plugin/ClassSourceFactoryTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2006-present the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.codehaus.gmaven.plugin;
17 |
18 | import java.io.File;
19 | import java.net.URL;
20 |
21 | import org.sonatype.sisu.litmus.testsupport.TestSupport;
22 |
23 | import org.codehaus.gmaven.adapter.ClassSource;
24 | import org.junit.Before;
25 | import org.junit.Test;
26 |
27 | import static org.hamcrest.Matchers.is;
28 | import static org.hamcrest.Matchers.notNullValue;
29 | import static org.hamcrest.Matchers.nullValue;
30 | import static org.junit.Assert.assertThat;
31 |
32 | /**
33 | * Tests for {@link ClassSourceFactory}.
34 | */
35 | public class ClassSourceFactoryTest
36 | extends TestSupport
37 | {
38 | private ClassSourceFactory underTest;
39 |
40 | @Before
41 | public void setUp() throws Exception {
42 | underTest = new ClassSourceFactory();
43 | }
44 |
45 | private String whitespace(final String text) {
46 | return "\n\t " + text + "\n\t ";
47 | }
48 |
49 | private void assertUrl(final String url) throws Exception {
50 | ClassSource source = underTest.create(url);
51 | log(source);
52 |
53 | assertThat(source, notNullValue());
54 | assertThat(source.getUrl(), notNullValue());
55 | assertThat(source.getFile(), nullValue());
56 | assertThat(source.getInline(), nullValue());
57 |
58 | assertThat(source.getUrl(), is(new URL(url)));
59 | }
60 |
61 | @Test
62 | public void create_url() throws Exception {
63 | assertUrl("http://google.com");
64 | }
65 |
66 | @Test
67 | public void create_url_withWhitespace() throws Exception {
68 | assertUrl(whitespace("http://google.com"));
69 | }
70 |
71 | private void assertFile(final String path) throws Exception {
72 | ClassSource source = underTest.create(path);
73 | log(source);
74 |
75 | assertThat(source, notNullValue());
76 | assertThat(source.getUrl(), nullValue());
77 | assertThat(source.getFile(), notNullValue());
78 | assertThat(source.getInline(), nullValue());
79 |
80 | assertThat(source.getFile(), is(new File(path.trim())));
81 | }
82 |
83 | @Test
84 | public void create_file() throws Exception {
85 | assertFile(util.createTempFile().getPath());
86 | }
87 |
88 | @Test
89 | public void create_file_withWhitespace() throws Exception {
90 | assertFile(whitespace(util.createTempFile().getPath()));
91 | }
92 |
93 | // FIXME: This test will not pass as current logic treats non-existing files as inline-sources
94 |
95 | //@Test
96 | //public void create_file_nonExisting() throws Exception {
97 | // File file = util.createTempFile();
98 | // file.delete();
99 | // assertFile(file.getPath());
100 | //}
101 |
102 | private void assertInline(final String script) throws Exception {
103 | ClassSource source = underTest.create(script);
104 | log(source);
105 |
106 | assertThat(source, notNullValue());
107 | assertThat(source.getUrl(), nullValue());
108 | assertThat(source.getFile(), nullValue());
109 | assertThat(source.getInline(), notNullValue());
110 |
111 | // keep IDEA happy
112 | assert source.getInline() != null;
113 |
114 | assertThat(source.getInline().getName(), notNullValue());
115 | assertThat(source.getInline().getCodeBase(), notNullValue());
116 | assertThat(source.getInline().getInput(), notNullValue());
117 | }
118 |
119 | @Test
120 | public void create_inline() throws Exception {
121 | assertInline("println 1234");
122 | }
123 |
124 | @Test
125 | public void create_inline_withWhitespace() throws Exception {
126 | assertInline(whitespace("println 1234"));
127 | }
128 |
129 | @Test
130 | public void create_inlineIncrementsCounter() {
131 | ClassSourceFactory.scriptCounter.set(0);
132 |
133 | underTest.create("println 1234");
134 | assertThat(ClassSourceFactory.scriptCounter.get(), is(1));
135 |
136 | underTest.create("println 5678");
137 | assertThat(ClassSourceFactory.scriptCounter.get(), is(2));
138 | }
139 | }
140 |
--------------------------------------------------------------------------------
/groovy-maven-plugin/src/test/java/org/codehaus/gmaven/plugin/ClasspathScopeTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2006-present the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.codehaus.gmaven.plugin;
17 |
18 | import org.sonatype.sisu.litmus.testsupport.TestSupport;
19 |
20 | import org.junit.Test;
21 |
22 | import static org.codehaus.gmaven.plugin.ClasspathScope.compile;
23 | import static org.codehaus.gmaven.plugin.ClasspathScope.none;
24 | import static org.codehaus.gmaven.plugin.ClasspathScope.provided;
25 | import static org.codehaus.gmaven.plugin.ClasspathScope.runtime;
26 | import static org.codehaus.gmaven.plugin.ClasspathScope.test;
27 | import static org.hamcrest.Matchers.is;
28 | import static org.junit.Assert.assertThat;
29 |
30 | /**
31 | * Tests for {@link ClasspathScope}.
32 | */
33 | public class ClasspathScopeTest
34 | extends TestSupport
35 | {
36 | @Test
37 | public void matches_none() {
38 | ClasspathScope value = none;
39 | assertThat(value.matches(none), is(true));
40 | assertThat(value.matches(provided), is(false));
41 | assertThat(value.matches(compile), is(false));
42 | assertThat(value.matches(runtime), is(false));
43 | assertThat(value.matches(test), is(false));
44 | }
45 |
46 | @Test
47 | public void matches_provided() {
48 | ClasspathScope value = provided;
49 | assertThat(value.matches(none), is(false));
50 | assertThat(value.matches(provided), is(true));
51 | assertThat(value.matches(compile), is(false));
52 | assertThat(value.matches(runtime), is(false));
53 | assertThat(value.matches(test), is(false));
54 | }
55 |
56 | @Test
57 | public void matches_compile() {
58 | ClasspathScope value = compile;
59 | assertThat(value.matches(none), is(false));
60 | assertThat(value.matches(provided), is(true));
61 | assertThat(value.matches(compile), is(true));
62 | assertThat(value.matches(runtime), is(false));
63 | assertThat(value.matches(test), is(false));
64 | }
65 |
66 | @Test
67 | public void matches_runtime() {
68 | ClasspathScope value = runtime;
69 | assertThat(value.matches(none), is(false));
70 | assertThat(value.matches(provided), is(true));
71 | assertThat(value.matches(compile), is(true));
72 | assertThat(value.matches(runtime), is(true));
73 | assertThat(value.matches(test), is(false));
74 | }
75 |
76 | @Test
77 | public void matches_test() {
78 | ClasspathScope value = test;
79 | assertThat(value.matches(none), is(false));
80 | assertThat(value.matches(provided), is(true));
81 | assertThat(value.matches(compile), is(true));
82 | assertThat(value.matches(runtime), is(true));
83 | assertThat(value.matches(test), is(true));
84 | }
85 | }
86 |
--------------------------------------------------------------------------------
/groovy-maven-plugin/src/test/java/org/codehaus/gmaven/plugin/FailClosureTargetTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2006-present the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.codehaus.gmaven.plugin;
17 |
18 | import org.sonatype.sisu.litmus.testsupport.TestSupport;
19 |
20 | import org.apache.maven.plugin.MojoExecutionException;
21 | import org.junit.Before;
22 | import org.junit.Test;
23 |
24 | import static org.hamcrest.Matchers.is;
25 | import static org.hamcrest.Matchers.nullValue;
26 | import static org.junit.Assert.assertThat;
27 |
28 | /**
29 | * Tests for {@link FailClosureTarget}.
30 | */
31 | public class FailClosureTargetTest
32 | extends TestSupport
33 | {
34 | private FailClosureTarget underTest;
35 |
36 | @Before
37 | public void setUp() throws Exception {
38 | underTest = new FailClosureTarget();
39 | }
40 |
41 | @Test
42 | public void fail_null() throws Exception {
43 | try {
44 | underTest.call(null);
45 | }
46 | catch (MojoExecutionException e) {
47 | assertThat(e.getMessage(), is(FailClosureTarget.FAILED));
48 | }
49 | }
50 |
51 | @Test
52 | public void fail_noArgs() throws Exception {
53 | try {
54 | underTest.call(new Object[0]);
55 | }
56 | catch (MojoExecutionException e) {
57 | assertThat(e.getMessage(), is(FailClosureTarget.FAILED));
58 | }
59 | }
60 |
61 | @Test
62 | public void fail_object() throws Exception {
63 | try {
64 | underTest.call(new Object[]{"foo"});
65 | }
66 | catch (MojoExecutionException e) {
67 | assertThat(e.getMessage(), is("foo"));
68 | assertThat(e.getCause(), nullValue());
69 | }
70 | }
71 |
72 | @Test
73 | public void fail_throwable() throws Exception {
74 | @SuppressWarnings("ThrowableInstanceNeverThrown")
75 | Throwable cause = new Throwable("foo");
76 | try {
77 | underTest.call(new Object[]{cause});
78 | }
79 | catch (MojoExecutionException e) {
80 | assertThat(e.getMessage(), is("foo"));
81 | assertThat(e.getCause(), is(cause));
82 | }
83 | }
84 |
85 | @Test
86 | public void fail_objectAndThrowable() throws Exception {
87 | @SuppressWarnings("ThrowableInstanceNeverThrown")
88 | Throwable cause = new Throwable("bar");
89 | try {
90 | underTest.call(new Object[]{"foo", cause});
91 | }
92 | catch (MojoExecutionException e) {
93 | assertThat(e.getMessage(), is("foo"));
94 | assertThat(e.getCause(), is(cause));
95 | }
96 | }
97 |
98 | /**
99 | * 2-arg form, 2nd arg must be a Throwable.
100 | */
101 | @Test(expected = Error.class)
102 | public void fail_invalidArgs() throws Exception {
103 | underTest.call(new Object[]{1, 2});
104 | }
105 |
106 | /**
107 | * more than 2 args is not supported.
108 | */
109 | @Test(expected = Error.class)
110 | public void fail_tooManyArgs() throws Exception {
111 | underTest.call(new Object[]{1, 2, 3});
112 | }
113 | }
114 |
--------------------------------------------------------------------------------
/groovy-maven-plugin/src/test/java/org/codehaus/gmaven/plugin/GroovyRuntimeFactoryTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2006-present the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.codehaus.gmaven.plugin;
17 |
18 | import java.util.ArrayList;
19 | import java.util.Iterator;
20 | import java.util.List;
21 |
22 | import org.sonatype.sisu.litmus.testsupport.TestSupport;
23 |
24 | import org.codehaus.gmaven.adapter.GroovyRuntime;
25 | import org.junit.Before;
26 | import org.junit.Test;
27 | import org.mockito.Mock;
28 |
29 | import static org.hamcrest.Matchers.notNullValue;
30 | import static org.junit.Assert.assertThat;
31 | import static org.junit.Assert.fail;
32 | import static org.mockito.Mockito.mock;
33 |
34 | /**
35 | * Tests for {@link GroovyRuntimeFactory}.
36 | *
37 | * These tests assume that the underlying {@link java.util.ServiceLoader} functions,
38 | * so we by-pass it and only validate the handling around the loader.
39 | */
40 | public class GroovyRuntimeFactoryTest
41 | extends TestSupport
42 | {
43 | private GroovyRuntimeFactory underTest;
44 |
45 | @Mock
46 | private ClassLoader classLoader;
47 |
48 | private List services;
49 |
50 | @Before
51 | public void setUp() throws Exception {
52 | services = new ArrayList();
53 |
54 | underTest = new GroovyRuntimeFactory()
55 | {
56 | @Override
57 | protected Iterator findServices(final ClassLoader classLoader) {
58 | return services.iterator();
59 | }
60 | };
61 | }
62 |
63 | /**
64 | * If no service is found, ISE should be thrown.
65 | */
66 | @Test(expected = IllegalStateException.class)
67 | public void create_noServices() {
68 | underTest.create(classLoader);
69 | fail();
70 | }
71 |
72 | /**
73 | * If more than one service is found, ISE should be thrown.
74 | */
75 | @Test(expected = IllegalStateException.class)
76 | public void create_multipuleServices() {
77 | services.add(mock(GroovyRuntime.class));
78 | services.add(mock(GroovyRuntime.class));
79 | underTest.create(classLoader);
80 | fail();
81 | }
82 |
83 | /**
84 | * Single service should return non-null value.
85 | */
86 | @Test
87 | public void create_singleService() {
88 | services.add(mock(GroovyRuntime.class));
89 | GroovyRuntime runtime = underTest.create(classLoader);
90 | assertThat(runtime, notNullValue());
91 | }
92 | }
93 |
--------------------------------------------------------------------------------
/groovy-maven-plugin/src/test/java/org/codehaus/gmaven/plugin/MojoSupportTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2006-present the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.codehaus.gmaven.plugin;
17 |
18 | import java.util.concurrent.atomic.AtomicInteger;
19 |
20 | import org.sonatype.sisu.litmus.testsupport.TestSupport;
21 |
22 | import org.apache.maven.plugin.MojoExecutionException;
23 | import org.apache.maven.plugin.MojoFailureException;
24 | import org.junit.Before;
25 | import org.junit.Test;
26 |
27 | import static org.hamcrest.Matchers.instanceOf;
28 | import static org.hamcrest.Matchers.is;
29 | import static org.junit.Assert.assertThat;
30 | import static org.junit.Assert.fail;
31 |
32 | /**
33 | * Tests for {@link MojoSupport}.
34 | */
35 | public class MojoSupportTest
36 | extends TestSupport
37 | {
38 | private AtomicInteger prepareCount = new AtomicInteger(0);
39 |
40 | private AtomicInteger runCount = new AtomicInteger(0);
41 |
42 | private AtomicInteger cleanupCount = new AtomicInteger(0);
43 |
44 | private class MojoSupportTester
45 | extends MojoSupport
46 | {
47 | @Override
48 | protected void prepare() throws Exception {
49 | prepareCount.incrementAndGet();
50 | }
51 |
52 | @Override
53 | protected void run() throws Exception {
54 | runCount.incrementAndGet();
55 | }
56 |
57 | @Override
58 | protected void cleanup() throws Exception {
59 | cleanupCount.incrementAndGet();
60 | }
61 | }
62 |
63 | @Before
64 | public void setUp() throws Exception {
65 | prepareCount.set(0);
66 | runCount.set(0);
67 | cleanupCount.set(0);
68 | }
69 |
70 | @Test
71 | public void execute_prepare_run_cleanup() throws Exception {
72 | MojoSupport underTest = new MojoSupportTester();
73 |
74 | underTest.execute();
75 |
76 | assertThat(prepareCount.get(), is(1));
77 | assertThat(runCount.get(), is(1));
78 | assertThat(cleanupCount.get(), is(1));
79 | }
80 |
81 | @Test
82 | public void execute_prepareExceptionCallsCleanup() throws Exception {
83 | MojoSupport underTest = new MojoSupportTester()
84 | {
85 | @Override
86 | protected void prepare() throws Exception {
87 | throw new Exception("fail");
88 | }
89 | };
90 |
91 | try {
92 | underTest.execute();
93 | fail();
94 | }
95 | catch (Exception e) {
96 | // expected
97 | }
98 |
99 | assertThat(runCount.get(), is(0));
100 | assertThat(cleanupCount.get(), is(1));
101 | }
102 |
103 | @Test
104 | public void execute_runExceptionCallsCleanup() throws Exception {
105 | MojoSupport underTest = new MojoSupportTester()
106 | {
107 | @Override
108 | protected void run() throws Exception {
109 | throw new Exception("fail");
110 | }
111 | };
112 |
113 | try {
114 | underTest.execute();
115 | fail();
116 | }
117 | catch (Exception e) {
118 | // expected
119 | }
120 |
121 | assertThat(prepareCount.get(), is(1));
122 | assertThat(cleanupCount.get(), is(1));
123 | }
124 |
125 | @Test
126 | public void execute_MojoExecutionException_propagates() throws Exception {
127 | MojoSupport underTest = new MojoSupportTester()
128 | {
129 | @Override
130 | protected void run() throws Exception {
131 | throw new MojoExecutionException("fail");
132 | }
133 | };
134 |
135 | try {
136 | underTest.execute();
137 | fail();
138 | }
139 | catch (Exception e) {
140 | assertThat(e, instanceOf(MojoExecutionException.class));
141 | }
142 | }
143 |
144 | @Test
145 | public void execute_MojoFailureException_propagates() throws Exception {
146 | MojoSupport underTest = new MojoSupportTester()
147 | {
148 | @Override
149 | protected void run() throws Exception {
150 | throw new MojoFailureException("fail");
151 | }
152 | };
153 |
154 | try {
155 | underTest.execute();
156 | fail();
157 | }
158 | catch (Exception e) {
159 | assertThat(e, instanceOf(MojoFailureException.class));
160 | }
161 | }
162 | }
163 |
--------------------------------------------------------------------------------
/groovy-maven-plugin/src/test/java/org/codehaus/gmaven/plugin/util/Maps2Test.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2006-present the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.codehaus.gmaven.plugin.util;
17 |
18 | import java.util.Enumeration;
19 | import java.util.Map;
20 | import java.util.Properties;
21 |
22 | import org.sonatype.sisu.litmus.testsupport.TestSupport;
23 |
24 | import com.google.common.collect.ImmutableList;
25 | import com.google.common.collect.Iterators;
26 | import org.junit.Test;
27 |
28 | import static org.hamcrest.Matchers.hasEntry;
29 | import static org.junit.Assert.assertThat;
30 |
31 | /**
32 | * {@link Maps2} tests.
33 | */
34 | public class Maps2Test
35 | extends TestSupport
36 | {
37 | @Test
38 | public void fromProperties_entryWithNullValue() throws Exception {
39 | Properties props1 = new Properties() {
40 | @Override
41 | public Enumeration> propertyNames() {
42 | return Iterators.asEnumeration(ImmutableList.of("foo").iterator());
43 | }
44 |
45 | @Override
46 | public String getProperty(final String key) {
47 | if ("foo".equals(key)) {
48 | return null;
49 | }
50 | return super.getProperty(key);
51 | }
52 | };
53 | Map props2 = Maps2.fromProperties(props1);
54 | assertThat(props2, hasEntry("foo", null));
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/groovy-maven-plugin/src/test/java/org/codehaus/gmaven/plugin/util/PropertiesBuilderTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2006-present the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.codehaus.gmaven.plugin.util;
17 |
18 | import java.util.Map;
19 |
20 | import org.sonatype.sisu.litmus.testsupport.TestSupport;
21 |
22 | import com.google.common.collect.ImmutableMap;
23 | import org.junit.Before;
24 | import org.junit.Test;
25 |
26 | import static org.hamcrest.Matchers.hasEntry;
27 | import static org.junit.Assert.assertThat;
28 |
29 | /**
30 | * Tests for {@link PropertiesBuilder}.
31 | */
32 | public class PropertiesBuilderTest
33 | extends TestSupport
34 | {
35 | private PropertiesBuilder underTest;
36 |
37 | @Before
38 | public void setUp() throws Exception {
39 | underTest = new PropertiesBuilder();
40 | }
41 |
42 | @Test
43 | public void defaults_used() throws Exception {
44 | underTest.setDefaults(ImmutableMap.of("foo", "bar"));
45 | underTest.setProperties(ImmutableMap.of("baz", "ick"));
46 | Map props = underTest.build();
47 |
48 | assertThat(props, hasEntry("foo", "bar"));
49 | }
50 |
51 | @Test
52 | public void properties_overrideDefaults() throws Exception {
53 | underTest.setDefaults(ImmutableMap.of("foo", "bar"));
54 | underTest.setProperties(ImmutableMap.of("foo", "baz"));
55 | Map props = underTest.build();
56 |
57 | assertThat(props, hasEntry("foo", "baz"));
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/groovy-maven-plugin/src/test/java/org/codehaus/gmaven/plugin/util/VersionHelperTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2006-present the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.codehaus.gmaven.plugin.util;
17 |
18 | import org.sonatype.sisu.litmus.testsupport.TestSupport;
19 |
20 | import org.eclipse.aether.version.VersionRange;
21 | import org.junit.Before;
22 | import org.junit.Test;
23 |
24 | import static org.hamcrest.Matchers.is;
25 | import static org.junit.Assert.assertThat;
26 |
27 | /**
28 | * Tests for {@link VersionHelper}.
29 | */
30 | public class VersionHelperTest
31 | extends TestSupport
32 | {
33 | private VersionHelper underTest;
34 |
35 | @Before
36 | public void setUp() throws Exception {
37 | underTest = new VersionHelper();
38 | }
39 |
40 | @Test
41 | public void range() {
42 | VersionRange range = underTest.range(1, 2, 3);
43 | log(range);
44 | assertThat(range.toString(), is("[1.2.3,1.2.4)"));
45 | }
46 |
47 | @Test
48 | public void before() {
49 | VersionRange range = underTest.before(1, 2, 3);
50 | log(range);
51 | assertThat(range.toString(), is("(,1.2.3-" + VersionHelper.EARLIEST_SUFFIX + ")"));
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/header.txt:
--------------------------------------------------------------------------------
1 | Copyright (c) ${project.inceptionYear}-present the original author or authors.
2 |
3 | Licensed under the Apache License, Version 2.0 (the "License");
4 | you may not use this file except in compliance with the License.
5 | You may obtain a copy of the License at
6 |
7 | http://www.apache.org/licenses/LICENSE-2.0
8 |
9 | Unless required by applicable law or agreed to in writing, software
10 | distributed under the License is distributed on an "AS IS" BASIS,
11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | See the License for the specific language governing permissions and
13 | limitations under the License.
--------------------------------------------------------------------------------
/mvnw.cmd:
--------------------------------------------------------------------------------
1 | @REM ----------------------------------------------------------------------------
2 | @REM Licensed to the Apache Software Foundation (ASF) under one
3 | @REM or more contributor license agreements. See the NOTICE file
4 | @REM distributed with this work for additional information
5 | @REM regarding copyright ownership. The ASF licenses this file
6 | @REM to you under the Apache License, Version 2.0 (the
7 | @REM "License"); you may not use this file except in compliance
8 | @REM with the License. You may obtain a copy of the License at
9 | @REM
10 | @REM http://www.apache.org/licenses/LICENSE-2.0
11 | @REM
12 | @REM Unless required by applicable law or agreed to in writing,
13 | @REM software distributed under the License is distributed on an
14 | @REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 | @REM KIND, either express or implied. See the License for the
16 | @REM specific language governing permissions and limitations
17 | @REM under the License.
18 | @REM ----------------------------------------------------------------------------
19 |
20 | @REM ----------------------------------------------------------------------------
21 | @REM Maven2 Start Up Batch script
22 | @REM
23 | @REM Required ENV vars:
24 | @REM JAVA_HOME - location of a JDK home dir
25 | @REM
26 | @REM Optional ENV vars
27 | @REM M2_HOME - location of maven2's installed home dir
28 | @REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands
29 | @REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a key stroke before ending
30 | @REM MAVEN_OPTS - parameters passed to the Java VM when running Maven
31 | @REM e.g. to debug Maven itself, use
32 | @REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
33 | @REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files
34 | @REM ----------------------------------------------------------------------------
35 |
36 | @REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on'
37 | @echo off
38 | @REM set title of command window
39 | title %0
40 | @REM enable echoing my setting MAVEN_BATCH_ECHO to 'on'
41 | @if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO%
42 |
43 | @REM set %HOME% to equivalent of $HOME
44 | if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%")
45 |
46 | @REM Execute a user defined script before this one
47 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre
48 | @REM check for pre script, once with legacy .bat ending and once with .cmd ending
49 | if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat"
50 | if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd"
51 | :skipRcPre
52 |
53 | @setlocal
54 |
55 | set ERROR_CODE=0
56 |
57 | @REM To isolate internal variables from possible post scripts, we use another setlocal
58 | @setlocal
59 |
60 | @REM ==== START VALIDATION ====
61 | if not "%JAVA_HOME%" == "" goto OkJHome
62 |
63 | echo.
64 | echo Error: JAVA_HOME not found in your environment. >&2
65 | echo Please set the JAVA_HOME variable in your environment to match the >&2
66 | echo location of your Java installation. >&2
67 | echo.
68 | goto error
69 |
70 | :OkJHome
71 | if exist "%JAVA_HOME%\bin\java.exe" goto init
72 |
73 | echo.
74 | echo Error: JAVA_HOME is set to an invalid directory. >&2
75 | echo JAVA_HOME = "%JAVA_HOME%" >&2
76 | echo Please set the JAVA_HOME variable in your environment to match the >&2
77 | echo location of your Java installation. >&2
78 | echo.
79 | goto error
80 |
81 | @REM ==== END VALIDATION ====
82 |
83 | :init
84 |
85 | @REM Find the project base dir, i.e. the directory that contains the folder ".mvn".
86 | @REM Fallback to current working directory if not found.
87 |
88 | set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR%
89 | IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir
90 |
91 | set EXEC_DIR=%CD%
92 | set WDIR=%EXEC_DIR%
93 | :findBaseDir
94 | IF EXIST "%WDIR%"\.mvn goto baseDirFound
95 | cd ..
96 | IF "%WDIR%"=="%CD%" goto baseDirNotFound
97 | set WDIR=%CD%
98 | goto findBaseDir
99 |
100 | :baseDirFound
101 | set MAVEN_PROJECTBASEDIR=%WDIR%
102 | cd "%EXEC_DIR%"
103 | goto endDetectBaseDir
104 |
105 | :baseDirNotFound
106 | set MAVEN_PROJECTBASEDIR=%EXEC_DIR%
107 | cd "%EXEC_DIR%"
108 |
109 | :endDetectBaseDir
110 |
111 | IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig
112 |
113 | @setlocal EnableExtensions EnableDelayedExpansion
114 | for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a
115 | @endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS%
116 |
117 | :endReadAdditionalConfig
118 |
119 | SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe"
120 | set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar"
121 | set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain
122 |
123 | set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.4.2/maven-wrapper-0.4.2.jar"
124 | FOR /F "tokens=1,2 delims==" %%A IN (%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties) DO (
125 | IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B
126 | )
127 |
128 | @REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central
129 | @REM This allows using the maven wrapper in projects that prohibit checking in binary data.
130 | if exist %WRAPPER_JAR% (
131 | echo Found %WRAPPER_JAR%
132 | ) else (
133 | echo Couldn't find %WRAPPER_JAR%, downloading it ...
134 | echo Downloading from: %DOWNLOAD_URL%
135 | powershell -Command "(New-Object Net.WebClient).DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"
136 | echo Finished downloading %WRAPPER_JAR%
137 | )
138 | @REM End of extension
139 |
140 | %MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %*
141 | if ERRORLEVEL 1 goto error
142 | goto end
143 |
144 | :error
145 | set ERROR_CODE=1
146 |
147 | :end
148 | @endlocal & set ERROR_CODE=%ERROR_CODE%
149 |
150 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost
151 | @REM check for post script, once with legacy .bat ending and once with .cmd ending
152 | if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat"
153 | if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd"
154 | :skipRcPost
155 |
156 | @REM pause the script if MAVEN_BATCH_PAUSE is set to 'on'
157 | if "%MAVEN_BATCH_PAUSE%" == "on" pause
158 |
159 | if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE%
160 |
161 | exit /B %ERROR_CODE%
162 |
--------------------------------------------------------------------------------
/src/site/markdown/index.md:
--------------------------------------------------------------------------------
1 |
18 | # GMaven
19 |
20 |
21 |
22 | [Groovy](http://groovy.codehaus.org) integration for [Apache Maven](http://maven.apache.org).
23 |
24 | ## Supported Environments
25 |
26 | * Java 1.6+
27 | * Groovy 2.0+
28 | * Groovy 2.1+
29 | * Apache Maven 3.0+
30 | * Apache Maven 3.1+
31 |
32 | ## Features
33 |
34 | * Script execution
35 | * GUI console access
36 | * Command-line shell access
37 |
38 | For more details please see the [groovy-maven-plugin](groovy-maven-plugin/index.html) documentation.
39 |
40 | ## No Compilation Support
41 |
42 | GMaven 2.x no longer supports any integration for compilation of Groovy sources. There were too many problems with
43 | stub-generation and hooking up compliation to the proper Maven lifecycle phases to effectivly support.
44 |
45 | For compliation integration with Maven please see the
46 | [Groovy Eclipse Compiler](https://github.com/groovy/groovy-eclipse/wiki/Groovy-Eclipse-Maven-plugin),
47 | which is the recommended and prefered option. If any problems are discovered with the compiler please
48 | [report an issue](https://github.com/groovy/groovy-eclipse/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc).
49 |
50 | As a fallback the [Groovy ant tasks](http://groovy.codehaus.org/Compiling+With+Maven2) can also be used
51 | if for some reason the prefered option is not viable for the target environment.
52 |
--------------------------------------------------------------------------------
/src/site/publish.groovy:
--------------------------------------------------------------------------------
1 | //
2 | // Helper to checkout, update, commit and push Maven-generated site content for GH-Pages.
3 | //
4 |
5 | // FIXME: ATM only supports github SSH repository urls
6 | // TODO: allow configuration to be passed in from pom
7 |
8 | boolean dryRun = Boolean.parseBoolean(properties.getProperty('dryRun', 'false'))
9 | log.info "Dry run: $dryRun"
10 |
11 | def siteUrl = project?.distributionManagement?.site?.url
12 | log.info "Site URL: $siteUrl"
13 | assert siteUrl
14 |
15 | def gitRepository = siteUrl - 'scm:git:ssh://git@github.com/'
16 | log.info "GIT repository: $gitRepository"
17 |
18 | def gitUrl = "git@github.com:$gitRepository"
19 | log.info "GIT URL: $gitUrl"
20 |
21 | def gitBranch = properties.getProperty('gitBranch', 'gh-pages')
22 | log.info "GIT Branch: $gitBranch"
23 |
24 | def targetDir = new File(project.build.directory as String)
25 | log.info "Target directory: $targetDir"
26 |
27 | def stagingDir = new File(targetDir, 'staging')
28 | log.info "Staging directory: $stagingDir"
29 | assert stagingDir.exists()
30 |
31 | def checkoutDir = new File(targetDir, 'publish-checkout')
32 | log.info "Checkout directory: $checkoutDir"
33 | if (checkoutDir.exists()) {
34 | ant.delete(dir: checkoutDir)
35 | }
36 | ant.mkdir(dir: checkoutDir)
37 |
38 | // helper to run a task surrounded by snippet markers
39 | def snippet = { Closure task ->
40 | println '----8<----'
41 | task.run()
42 | println '---->8----'
43 | }
44 |
45 | log.info "Cloning $gitUrl ($gitBranch branch) to: $checkoutDir"
46 | snippet {
47 | ant.exec(executable: 'git', dir: checkoutDir) {
48 | arg(value: 'clone')
49 | arg(value: '--branch')
50 | arg(value: gitBranch)
51 | arg(value: gitUrl)
52 | arg(value: '.')
53 | }
54 | }
55 |
56 | log.info "Removing existing content from $checkoutDir"
57 | ant.exec(executable: 'git', dir: checkoutDir, failonerror: true) {
58 | arg(value: 'rm')
59 | arg(value: '-r')
60 | arg(value: '--quiet')
61 | arg(value: '.')
62 | }
63 |
64 | log.info "Copying generated content to: $checkoutDir"
65 | ant.copy(todir: checkoutDir) {
66 | fileset(dir: stagingDir) {
67 | include(name: '**')
68 | }
69 | }
70 |
71 | log.info 'Adding changed files'
72 | ant.exec(executable: 'git', dir: checkoutDir, failonerror: true) {
73 | arg(value: 'add')
74 | arg(value: '.')
75 | }
76 |
77 | log.info 'Checking status'
78 | ant.exec(executable: 'git', dir: checkoutDir, failonerror: true, outputproperty: 'git-status') {
79 | arg(value: 'status')
80 | arg(value: '--short')
81 | }
82 | def status = ant.project.properties.'git-status'.trim()
83 |
84 | if (status) {
85 | snippet { println status }
86 |
87 | log.info 'Committing changes'
88 | snippet {
89 | ant.exec(executable: 'git', dir: checkoutDir, failonerror: true) {
90 | arg(value: 'commit')
91 | arg(value: '-m')
92 | arg(value: 'Maven-generated site content refresh')
93 | }
94 | }
95 |
96 | if (!dryRun) {
97 | log.info 'Pushing changes'
98 | snippet {
99 | ant.exec(executable: 'git', dir: checkoutDir, failonerror: true) {
100 | arg(value: 'push')
101 | arg(value: 'origin')
102 | arg(value: gitBranch)
103 | }
104 | }
105 | }
106 | }
107 | else {
108 | log.info 'No changes detected'
109 | }
110 |
111 | log.info 'Done'
112 |
--------------------------------------------------------------------------------
/src/site/resources/images/gmaven.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/groovy/gmaven/b5fa664ecea354548d039d39e04011a44e6f5f4c/src/site/resources/images/gmaven.png
--------------------------------------------------------------------------------
/src/site/resources/images/groovy.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/groovy/gmaven/b5fa664ecea354548d039d39e04011a44e6f5f4c/src/site/resources/images/groovy.png
--------------------------------------------------------------------------------
/src/site/site.xml:
--------------------------------------------------------------------------------
1 |
2 |
19 |
21 |
22 |
23 | gmaven
24 | GMaven
25 | http://groovy.github.io/gmaven/images/gmaven.png
26 | http://groovy.github.io/gmaven
27 |
28 |
29 |
30 | org.apache.maven.skins
31 | maven-fluido-skin
32 | 1.7
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 | false
41 | true
42 |
43 | groovy/gmaven
44 | right
45 | green
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
58 |
59 |
62 |
65 |
66 |
71 |
72 |
75 |
85 |
86 |
90 |
91 |
--------------------------------------------------------------------------------