1 /* 2 * Copyright 2017-2021 original 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 * https://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 io.micronaut.build.testresources; 17 18 import org.apache.maven.model.Dependency; 19 import org.apache.maven.plugin.ContextEnabled; 20 import org.apache.maven.plugin.Mojo; 21 import org.apache.maven.plugin.logging.Log; 22 import org.apache.maven.plugin.logging.SystemStreamLog; 23 import org.apache.maven.plugins.annotations.Parameter; 24 25 import java.io.File; 26 import java.nio.file.Path; 27 import java.util.List; 28 import java.util.Map; 29 30 /** 31 * Base mojo for Micronaut test resources service handling. 32 */ 33 public abstract class AbstractTestResourcesMojo extends TestResourcesConfiguration implements Mojo, ContextEnabled { 34 35 private static final String DEFAULT_CLASSPATH_INFERENCE = "true"; 36 private static final String DEFAULT_CLIENT_TIMEOUT = "60"; 37 38 /** 39 * Instance logger. 40 */ 41 protected Log log; 42 43 /** 44 * Plugin container context. 45 */ 46 protected Map pluginContext; 47 48 @Parameter(defaultValue = "${project.build.directory}", required = true) 49 protected File buildDirectory; 50 51 /** 52 * Micronaut Test Resources version. Should be defined by the Micronaut BOM, but this parameter can be used to 53 * define a different version. 54 */ 55 @Parameter(property = CONFIG_PROPERTY_PREFIX + "version", required = true) 56 protected String testResourcesVersion; 57 58 /** 59 * If set to true, Micronaut will attempt to infer which dependencies should be added to the Test Resources server 60 * classpath, based on the project dependencies. In general the result will consist of modules from the 61 * test-resources project, but it may consist of additional entries, for example database drivers. 62 */ 63 @Parameter(defaultValue = DEFAULT_CLASSPATH_INFERENCE) 64 protected Boolean classpathInference = Boolean.valueOf(DEFAULT_CLASSPATH_INFERENCE); 65 66 /** 67 * Additional dependencies to add to the Test Resources server classpath when not using classpath inference, or when 68 * the inference doesn't produce the desired result. 69 */ 70 @Parameter 71 protected List<Dependency> testResourcesDependencies; 72 73 /** 74 * By default, the Test Resources server will be started on a random (available) port, but it can be set a fixed port 75 * by using this parameter. 76 */ 77 @Parameter(property = CONFIG_PROPERTY_PREFIX + "port") 78 protected Integer explicitPort; 79 80 /** 81 * Configures the maximum amount of time to wait for the server to start a test resource. Some containeres may take 82 * a long amount of time to start with slow internet connections. 83 */ 84 @Parameter(property = CONFIG_PROPERTY_PREFIX + "client-timeout", defaultValue = DEFAULT_CLIENT_TIMEOUT) 85 protected Integer clientTimeout = Integer.valueOf(DEFAULT_CLIENT_TIMEOUT); 86 87 public static Path serverSettingsDirectoryOf(Path buildDir) { 88 return buildDir.resolve("../.micronaut/test-resources"); 89 } 90 91 /** 92 * @see org.apache.maven.plugin.Mojo#setLog(org.apache.maven.plugin.logging.Log) 93 */ 94 public void setLog(Log log) { 95 this.log = log; 96 } 97 98 /** 99 * Returns the logger that has been injected into this mojo. If no logger has been setup yet, a 100 * <code>SystemStreamLog</code> logger will be created and returned. 101 * <strong>Note:</strong> 102 * The logger returned by this method must not be cached in an instance field during the construction of the mojo. 103 * This would cause the mojo to use a wrongly configured default logger when being run by Maven. The proper logger 104 * gets injected by the Plexus container <em>after</em> the mojo has been constructed. Therefore, simply call this 105 * method directly whenever you need the logger, it is fast enough and needs no caching. 106 * 107 * @see org.apache.maven.plugin.Mojo#getLog() 108 */ 109 public Log getLog() { 110 if (log == null) { 111 log = new SystemStreamLog(); 112 } 113 return log; 114 } 115 116 /** 117 * @see org.apache.maven.plugin.ContextEnabled#getPluginContext() 118 */ 119 public Map getPluginContext() { 120 return pluginContext; 121 } 122 123 /** 124 * @see org.apache.maven.plugin.ContextEnabled#setPluginContext(java.util.Map) 125 */ 126 public void setPluginContext(Map pluginContext) { 127 this.pluginContext = pluginContext; 128 } 129 130 }