Use Maven to Edit the standalone.xml When Starting Wildfly - maven

I have set up a Maven project that starts up a standalone Wildfly container using Cargo, but in the process of adding some new features, I need to add some content to the standalone.xml. (Specifically, a security realm.) This seems like it should be a fairly common task, but I can't find a way to do it.
I tried to use the xml-replacement feature built in to cargo, but it doesn't seem to be capable of adding content, only modifying existing content. My second attempt was to use the maven-replacer-plugin, which feels like a hack, but would have done the job. The problem I have with it is that I can't find a way to execute the replacer plugin between when cargo installs the container, and when cargo starts the container.
Thanks in Advance

I'd recommend switching from Cargo to the wildfly-maven-plugin, which is container-specific but more straightforward to use.
You can run any CLI command via the plugin to edit your standalone.xml.
See this example from one of my projects.

Of course you can override the container's configuration, see here and there how you can do that.
In your case if you wanted to put a custom standalone.xml, you simply have to embed the entire file in your project and put it in the configFiles for cargo to deploy it with your container.

Related

Can I automate my spring-boot docker builds with the dockerfile or would Jenkins be more appropriate?

After first attempts of setting up automated docker builds for my personal spring-boot github repos (I'm a newbie) they were constantly failing due to the jar file not being found. While my first impression was that I had the pathing wrong, it dawned on me that docker wasn't building the jars before attempting the builds.
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE=target/myjar*.jar
ADD ${JAR_FILE} myjar.jar
EXPOSE 8080
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/myjar.jar"]
While I have found a couple of resouces that describe how to integrate maven into the Dockerfile, it seemed like my image could easily get too big too easily with that approach. Has anybody tackled this issue before and could recommend recommend a way to integrate maven into the Dockerfile build? An alternative I've given thought to is to learn Jenkins and develop a solution that way for a pipeline.
There are many approaches to achieve this.
Approach 1
Rely on the maven with its docker maven plugin
This indeed allows building the image during the maven build.
This approach will work in general and allows a level of customizations sufficient for
many use cases. Usually, spring boot applications come as a single Jar with all the dependencies packed inside it, so there is no need for multiple layers in docker image (at least in my experience).
The point here is to call the plugin when the jar of spring boot is already built.
The jar is prepared with the help of another plugin and it is usually invoked in package phase, so if you go with this approach, make sure that you invoke the image creation plugin after the spring boot maven plugin that builds the single jar artifact.
The artifact must reside in <you_module>/target folder and it will be pretty big in size. The original module will reside next to it but will have the suffix .original.
Approach 2
Let maven build the artifact but not the image. Maven will end after this step.
Then invoke a script in Jenkins that will run the docker build command and it will build the image.
The result will be the same, you'll work here with docker directly possibly utilizing all its perks if you really need it.
Both approaches can work for you, choosing between them depends on the following factors:
You're more a Java / maven guy rather than devops guy - then go with approach 1 otherwise go with approach 2
You would like to actually locally run the image that you've built, for example, if you have a kubernetes cluster installed locally - in this case, go with approach 1
You need to utilize some latest features of docker not available for use from the plugin - then go with approach 2

How to get IntelliJ IDEA run configuration to make use of Maven filtered resources?

I'm having some trouble working out how to run a project in IntelliJ IDEA that makes use of Maven resource filtering.
In my src/main/resources directory I have
spring.xml file that is used to start the application
service.env.properties
I also have the following in src/main/filters
env-uat.properties
env-prd.properties
env-dev.properties
In my pom.xml file I use maven-resources-plugin to create three configuration directories that get included into the final package. Each one is based on one of the filers. This package can then be deployed on any of the three environments without having to be rebuilt.
The issue that I'm having is when I try to run this using IntelliJ IDEA run configs I have to use
-Xbootclasspath/a:target/config/dev;
to force IntelliJ IDEA to pick up the .properties file with the correct filter.
While this works it doesn't seem like it is the right way to go here. Does anyone have any advice on how to handle configuration for multiple environments and how to get IntelliJ IDEA to pick this up through run configurations.

How to download given dependency in Gradle

I am running Gradle builds in a Docker container and I wanted to create a Docker image that would already contain all most common dependencies, so the build itself does not need to download them.
Is there an easy way to tell Gradle to download particular library (or plugin) with all dependencies without a specific build file? I want to use the image to run different builds that do not share any configuration.
I am looking for something similar to Maven's dependency:get.
I did not find any solutions other than creating a small build script in Gradle with some dependencies defined there and running build command.
However, it was easier to just mount Gradle cache as a volume in the container I was using. That has added benefit of being reusable accross many Docker containers. This is the approach I ended up using, if anyone is interested.

How to easily copy/rename/remove files with Maven (as in Ant)

I am working on a project and using Maven to build it. The project is a quite big Java web application and it is supposed to work with both Mysql and Oracle databases.
The problem is that there are some specific annotations related to either of the two databases in the source code, plus some other differences, so that I am forced to manually comment/uncomment part of the code before building the application for one of the two databases.
Basically what I would like to achieve is to have my build script, maybe via a Maven profile, to automatically switch the source classes before building depending on the database I want my war to work against.
Putting it simply, the idea is to have MyClass.oracle and MyClass.mysql, and depending on my build profile I should move one of the two in the source dir, rename it MyClass and build. This should be done for some packages, classes, and also configuration files.
Is there any way I can achieve it via "pure" Maven? The only solution I came across till now is to use an antrun plugin and reference an Ant build.xml inside of it.
Thank you,
Mattia
A pure maven solution would be to develop your own maven plugin. Depending on your requirements this can be an overkill, however it is not hard at all, you can see how to achieve this here.
This is a limitation of Maven. One of Maven's purposes is to not have a build script. You should simply use the plugins as available, and setup your project the right way, and magically, everything will build!
There is one solution: Use Ant. Well, not to redo your whole project with Ant, but with the antrun plugin, you can run a few Ant tasks at various phases of your Maven build life cycle.
It's been a long, long time since I've used this, so I am not going to try to write a test pom.xml, but I don't remember it being very difficult to use.
Of course, the correct Maven solution is to divide your project up into "common core" code, and then a separate Oracle and MySql client that uses the "common core". By the way, I hope you're not patching source code. Instead, you're using a properties file to do this for you.

Maven cargo plugin - redeploy specific deployable in standalone container?

I'm currently working on a project that consists of several services written in Java that are accessed by a Ruby/Rails front-end. In an attempt to simplify local development, I've created a separate project that adds all of our service WAR projects as dependencies, and uses the cargo-maven-plugin to deploy each of these as a deployable inside of a single embedded Jetty instance.
The issue I'm having is that I'd like to be able to tell cargo to re-deploy a single WAR out of the several that are being run at a time. Starting the entire set of services from scratch takes a bit, and is really unnecessary when only one deployable has actually changed. As far as I can tell, the cargo:redeploy goal only works for non-standalone containers, and I also haven't been able to find any documentation that its possible to specify what you want to re-deploy on the command line.
Is there a way to tell cargo to re-deploy a single deployable from the command line? I'm thinking of something along the lines of mvn cargo:redeploy -DgroupId=com.foo.bar -DartifactId=baz
Apologies if this isn't clear, or if there is a different approach that I should be taking entirely - I'm relatively new to Java development and Maven.
Thanks for any help.
Download the latest war file to your local machine, then redeploy using the following pattern:
mvn install:install-file -DgroupId=com.foo.bar -DartifactId=baz -Dversion=1.x -Dpackaging=war -Dfile=C:/cargo.jar

Resources