001/* 002 * Copyright 2017-2021 original authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package io.micronaut.maven.testresources; 017 018import io.micronaut.maven.JansiLog; 019import org.apache.maven.model.Dependency; 020import org.apache.maven.plugin.ContextEnabled; 021import org.apache.maven.plugin.Mojo; 022import org.apache.maven.plugin.logging.Log; 023import org.apache.maven.plugin.logging.SystemStreamLog; 024import org.apache.maven.plugins.annotations.Parameter; 025 026import java.io.File; 027import java.nio.file.Path; 028import java.util.List; 029import java.util.Map; 030 031/** 032 * Base mojo for Micronaut test resources service handling. 033 */ 034public abstract class AbstractTestResourcesMojo extends TestResourcesConfiguration implements Mojo, ContextEnabled { 035 036 private static final String DEFAULT_CLASSPATH_INFERENCE = "true"; 037 private static final String DEFAULT_CLIENT_TIMEOUT = "60"; 038 039 /** 040 * Instance logger. 041 */ 042 protected Log log; 043 044 /** 045 * Plugin container context. 046 */ 047 protected Map<?, ?> pluginContext; 048 049 @Parameter(defaultValue = "${project.build.directory}", required = true) 050 protected File buildDirectory; 051 052 /** 053 * Micronaut Test Resources version. Should be defined by the Micronaut BOM, but this parameter can be used to 054 * define a different version. 055 */ 056 @Parameter(property = CONFIG_PROPERTY_PREFIX + "version", required = true) 057 protected String testResourcesVersion; 058 059 /** 060 * If set to true, Micronaut will attempt to infer which dependencies should be added to the Test Resources server 061 * classpath, based on the project dependencies. In general the result will consist of modules from the 062 * test-resources project, but it may consist of additional entries, for example database drivers. 063 */ 064 @Parameter(defaultValue = DEFAULT_CLASSPATH_INFERENCE) 065 protected Boolean classpathInference = Boolean.valueOf(DEFAULT_CLASSPATH_INFERENCE); 066 067 /** 068 * Additional dependencies to add to the Test Resources server classpath when not using classpath inference, or when 069 * the inference doesn't produce the desired result. 070 */ 071 @Parameter 072 protected List<Dependency> testResourcesDependencies; 073 074 /** 075 * By default, the Test Resources server will be started on a random (available) port, but it can be set a fixed port 076 * by using this parameter. 077 */ 078 @Parameter(property = CONFIG_PROPERTY_PREFIX + "port") 079 protected Integer explicitPort; 080 081 /** 082 * Configures the maximum amount of time to wait for the server to start a test resource. Some containers may take 083 * a long amount of time to start with slow internet connections. 084 */ 085 @Parameter(property = CONFIG_PROPERTY_PREFIX + "client-timeout", defaultValue = DEFAULT_CLIENT_TIMEOUT) 086 protected Integer clientTimeout = Integer.valueOf(DEFAULT_CLIENT_TIMEOUT); 087 088 /** 089 * Configures the duration after which the test resources service will automatically shut down if it doesn't 090 * get any request. 091 * 092 * @since 4.2.0 093 */ 094 @Parameter(property = CONFIG_PROPERTY_PREFIX + "server-idle-timeout-minutes") 095 protected Integer serverIdleTimeoutMinutes = Integer.valueOf(DEFAULT_CLIENT_TIMEOUT); 096 097 public static Path serverSettingsDirectoryOf(Path buildDir) { 098 return buildDir.resolve("../.micronaut/test-resources"); 099 } 100 101 @Override 102 public void setLog(Log log) { 103 this.log = new JansiLog(log); 104 } 105 106 @Override 107 public Log getLog() { 108 if (log == null) { 109 log = new SystemStreamLog(); 110 } 111 return log; 112 } 113 114 @Override 115 public Map<?, ?> getPluginContext() { 116 return pluginContext; 117 } 118 119 @Override 120 public void setPluginContext(Map pluginContext) { 121 this.pluginContext = pluginContext; 122 } 123 124}