How to switch conf tomcat with maven - java

I have ,in a java high envirgure project using Maven, to add integrations test. To do that, I must add a new test's database (minimalist true copy of the database). Until then it's good.
By against the problem is that I have two differents configurations in my file:
\web\target\tomcat\conf\context.xml
So I always put a ressource in a comment.
exemple of ressource :
<Resource
auth = "jdbc / soarepo"
driverClassName = "oracle.jdbc.driver.OracleDriver"
logAbandoned = "true"
maxActive = "32"
maxIdle = "32"
maxWait = "10000"
name = "jdbc / soarepo"
password = "..."
username = "..."
removeAbandoned = "true"
removeAbandonedTimeout = "60"
type = "javax.sql.DataSource"
url = "jdbc: oracle: thin: # ..."
/>
Q: How do I switch from one to another ressource with Maven?
I already use a profile for integration tests. So I have to change what resources when the profile is called ?

If you are using the Maven Tomcat Plugin to run your web application, you can specify the path of a custom server-dependent deployment descriptor, context.xml, using the contextFile property:
<profiles>
<profile>
<id>integration-tests</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<contextFile>/path/to/context/file</contextFile>
</configuration>
</plugin>
</plugins>
</build>
</profile>
.. Other profiles ..
</profiles>

Related

Spring profiles with Gradle

I'm running a spring boot project with three different property files (application.properties, application-prod.properties, application-uat.properties. I'm using Maven and if I don't specify any argument, it will pick the default property file which is application.properties. Here's how the related files look.
application.properties
#Set active profile
spring.profiles.active=#activatedProperties#
#MongoDB configuration
spring.data.mongodb.host=${MONGODB_HOST:localhost}
spring.data.mongodb.port=${MONGODB_PORT:27017}
spring.data.mongodb.database=database
spring.data.mongodb.username=username
spring.data.mongodb.password=password
pom.xml
<profiles>
<profile>
<id>UAT</id>
<properties>
<activatedProperties>uat</activatedProperties>
</properties>
</profile>
<profile>
<id>PROD</id>
<properties>
<activatedProperties>prod</activatedProperties>
</properties>
</profile>
</profiles>
How can I achieve the same with Gradle? How can I let Gradle to pick the default (application.properties) if I don't specify any arguement?
I think this link will be useful for you...
https://docs.gradle.org/current/userguide/migrating_from_maven.html#migmvn:profiles_and_properties
Sample:
if (!hasProperty('buildProfile')) ext.buildProfile = 'default'
apply from: "profile-${buildProfile}.gradle"
task greeting {
doLast {
println message
}
}
#profile-default.gradle
ext.message = 'foobar'
#profile-test.gradle
ext.message = 'testing 1 2 3'
#profile-prod.gradle
ext.message = 'Hello, world!'
This link can be useful too:
https://www.baeldung.com/spring-profiles

flyway.properties file not working as pom.xml configuration alternative

I need to set flyway migration and I don't want to put password and username in pom.xml, I created flyway.properties file, but it's not working, I'm getting this error
Failed to execute goal org.flywaydb:flyway-maven-plugin:6.5.1:clean (default-cli) on project entesting: org.flywaydb.core.api.FlywayException: Unable to connect to the database. Configure the url, user and password!
flyway.properties file is in same directory as pom.xml
flyway.user=sa
flyway.password=passwordForThis
flyway.url=jdbc:sqlserver://172.23.176.144;database=DB_Name
flyway.locations=filesystem:src/main/resources/db/migration
flyway.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
pom.xml flyway plugin config:
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>6.5.1</version>
<configuration>
<url>jdbc:sqlserver://172.23.176.144;database=DB_Name</url>
<user>sa</user>
<password>passwordForThis</password>
</configuration>
</plugin>
To summarize I don't want to add password, username etc. in pom.xml(that works), I want it to be in flyway.propeties.
Flyway can be configured in a number of ways beyond directly putting it into the maven pom.xml. See documentation on configuring the maven plugin
The contents of the properties file you showed looks like the contents of a flyway.conf file.
Flyway will search for and automatically load the <user-home>/flyway.conf config file if present.
It is also possible to point Flyway at one or more additional config files. This is achieved by supplying the System property flyway.configFiles as follows:
mvn -Dflyway.configFiles=path/to/myAlternativeConfig.conf flyway:migrate
See https://flywaydb.org/documentation/maven/#config-files for more information.
Alternatively for storing the database user and password, Maven settings.xml files can also be used:
<settings>
<servers>
<server>
<!-- By default Flyway will look for the server with the id 'flyway-db' -->
<!-- This can be customized by configuring the 'serverId' property -->
<id>flyway-db</id>
<username>myUser</username>
<password>mySecretPwd</password>
</server>
</servers>
</settings>

Maven Jib plugin: 401 Unauthorized

I am using Maven Jib plugin to deploy my application to Gitlab docker registry. If I use docker login registry.gitlab.com and enter username and password I can log in to the gitlab registry successfully. I can see that ~/.docker/config.json contains the following information:
{
"auths": {
"https://registry.gitlab.com": {},
"registry.gitlab.com": {}
},
"HttpHeaders": {
"User-Agent": "Docker-Client/19.03.2 (darwin)"
},
"credsStore": "desktop",
"stackOrchestrator": "swarm"
}
Now if I try to run mvn -Djib.to.auth.username=${username} -Djib.to.auth.password=${password} compile jib:build it fails with the 401 Unauthorized exception. The reason I need to pass the username and password is I would like to run this command in my CICD pipeline and it is required to pass the username and password somehow. I have tested other approaches like setting credentials in the .m2/settings.xml or pom.xml. None of them works.
You can have the credential in the jib plugin section as follows
<build>
<plugins>
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<from>
<image>openjdk:8u171-alpine</image>
</from>
<to>
<image>registry.gitlab.com/${project.build.finalName}:${project.version}</image>
<auth>
<username>${jib.username}</username>
<password>${jib.password}</password>
</auth>
</to>
</configuration>
</plugin>
<plugins>
<build>
Have the jib.username and jib.password as properties in settings.xml.
Note: Adjust the from image as to you need.

Parametrize webapp using Jenkins, Maven and JVM parameters

Two objectives:
I want to use parametrized jenkins builds to build a war file and set some properties during the build in the war file.
I also want to be able to refine those properties on the server, where the war file is deployed.
Number 1 is to set properties that fit to the target environment, Number 2 is to be able to quickly change them without having to rebuild the whole application again.
Choosing a maven profile is not flexible enough in this case.
An example would be a port number, that is different for every build, but can spontaneously be changed by the system administrator of the system where the file is deployed.
My idea was to use the maven-resource-filtering plugin to add the build parameters on build into property files. Then on startup of the webapp on the glassfish/tomcat to also look at the set JVM variables.
Am I thinking in the right direction?
After some research the solution for me looks as followed,
for a given parameter "test":
To objective 1.: Use command line parameters when executing maven build, e.g use:
install -Dtest=OnBuildValue
Then use the maven resources plugin, that replaces strings like test=${test} in property files, through the given parameter on build. Add:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>${maven.resources.version}</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
...
<build>
...
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>**/parameters.properties</exclude>
</excludes>
<filtering>false</filtering>
</resource>
</resources>
....
</build>
To objective 2.: To be able to change the parameter "test" in the container, without having to rebuild the war file, add a JVM parameter -DTEST=ContainerValue.
Now we need some logic:
private static String getBuildParameter(String paramName)
throws IOException {
Resource resource = new ClassPathResource(
"/META-INF/spring/parameters.properties");
Properties props = PropertiesLoaderUtils.loadProperties(resource);
return props.getProperty(paramName);
}
public static String getParameter(final String paramName,
final String defaultValue, final String logMessage) {
String value = System.getProperty(paramName);
if (value!=null) {
Logger.getLogger(ParameterManager.class.getName()).log(
Level.WARNING,
"Parameter: " + paramName + ": " + value
+ " found in JVM parameters.");
return value;
}
try {
value = getBuildParameter(paramName.toLowerCase());
} catch (IOException e) {
// catch, log exception...
}
if (value!=null) {
Logger.getLogger(ParameterManager.class.getName()).log(
Level.WARNING,
"Parameter: " + paramName + ": " + value
+ " found in parameters set on build time.");
return value;
}
Logger.getLogger(ParameterManager.class.getName()).log(
Level.WARNING,
"Parameter: " + paramName + ": " + defaultValue
+ " as default parameter. " + logMessage);
return defaultValue;
}
The methods are static because I use them to initialize constants like this:
public static final String TEST_STRING;
static {
TEST_STRING = getParameter("TEST", "default value",
"The default is set, please ensure, that this is intended!");
}
Those constants can then be read from everywhere inside your project. How you call this logic, or if you can implement it differently is up to you. I'm sure there are nicer ways, and I would be glad to hear about your implementation, but this works for me.
Note to 1.:
If you use Eclipse you can define the maven parameters under Run->Run configurations-> Goals.
BUT if you use the glassfish tools (m2e plugin) to deploy your project to your glassfish, it will not use the run configuration. You then have to create the file .m2/settings.xml looking like this:
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
https://maven.apache.org/xsd/settings-1.0.0.xsd>
<localRepository/>
<interactiveMode/>
<usePluginRegistry/>
<offline/>
<pluginGroups/>
<servers/>
<mirrors/>
<proxies/>
<profiles>
<profile>
<id>m2e</id>
<activation>
<property>
<name>m2e.version</name>
</property>
</activation>
<properties>
<test>xmlparameter</test>
</properties>
</profile>
</profiles>
<activeProfiles/>
</settings>

Set JARs to skip in order to speed up Tomcat 7 maven plugin startup

Is there a simple way (e.g., directly from tomcat7-maven-plugin configuration) to specify which JARs should be skipped during Tomcat startup in order to speed it up?
The Tomcat 7 documentation encourages to use a system property org.apache.catalina.startup.ContextConfig.jarsToSkip (see http://wiki.apache.org/tomcat/HowTo/FasterStartUp#JAR_scanning), but when set from maven configuration, it does not work.
After inspecting the sources of tomcat7-maven-plugin, I found a workaround to achieve jars skipping. (It may however stop working with future releases of Maven Tomcat 7 plugin.)
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<path>/${project.artifactId}</path>
<port>8080</port>
<systemProperties>
<org.apache.catalina.startup.ContextConfig.jarsToSkip>*</org.apache.catalina.startup.ContextConfig.jarsToSkip>
<!-- work around .. see: https://issues.apache.org/jira/browse/MTOMCAT-281 -->
<catalina.config>${project.baseUri}/target/tomcat/logs</catalina.config>
</systemProperties>
</configuration>
</plugin>
As described in a related bug (https://issues.apache.org/jira/browse/MTOMCAT-281), the problem is that Tomcat blindly overrides all system properties with properties from tomcat-embed-core-7.0.47.jar!/org/apache/catalina/startup/catalina.properties. As a result, the value of org.apache.catalina.startup.ContextConfig.jarsToSkip system property specified in plugin configuration is overridden.

Categories