Karaf - generating feature file and creating custom distribution - deployment

I have an application which I want to deploy in karaf. I have created a feature file and I am able to add features through this file using karaf console. What I want to achieve now is that create this feature file through maven commands instead of creating it manually and then create a custom karaf distribution using this feature file. How can I achieve it ?
My approach so far is to create a maven module for generating feature file using karaf-maven-plugin and then create another module to generate karaf custom distribution so that we dont need to access maven in production environment.
Is this approach correct ? Do I really need to make two different modules for achieving it. How can I get access to feature file from second module.
These are my poms -
all dependecies
<build>
<finalName>${project.artifactId}-${project.version}</finalName>
<plugins>
<plugin>
<groupId>org.apache.karaf.tooling</groupId>
<artifactId>karaf-maven-plugin</artifactId>
<version>4.0.5</version>
<extensions>true</extensions>
<executions>
<execution>
<id>generate</id>
<phase>generate-resources</phase>
<goals>
<goal>features-generate-descriptor</goal>
</goals>
<configuration>
<startLevel>80</startLevel>
<aggregateFeatures>true</aggregateFeatures>
<includeTransitiveDependency>true</includeTransitiveDependency>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
I am not able to figure out the second part yet. Any help with that is really appreciated.

To generate a custom Karaf you just need to use the karaf-maven-plugin.
For example the following will generate a fully working custom Karaf:
<plugin>
<groupId>org.apache.karaf.tooling</groupId>
<artifactId>karaf-maven-plugin</artifactId>
<version>4.0.0</version>
<extensions>true</extensions>
<configuration>
<!-- no startupFeatures -->
<bootFeatures>
<feature>minimal</feature>
</bootFeatures>
<installedFeatures>
<feature>wrapper</feature>
<feature>spring/4.0.7.RELEASE_1</feature>
</installedFeatures>
</configuration>
</plugin>
This will generate a custom karaf based on the minimal sets of features which are needed to create the minimal distro. If you want to depend on the standard distro just exchange that with standard.
Btw. all this is also documented in the Karaf documentation

Related

Spring REST Docs maven continuous build?

I was going through this tutorial https://www.youtube.com/watch?v=k5ncCJBarRI&t=1443s
Around 1:07:30 the author mentioned about "Gradle has continuous build" later on was able to detect changes in the test and automatically regenerate asciidoc. I was wondering if anyone knows how to set this up in maven?
I have looked through docs in spring and asciidoctor plugin, but was not able to find anything related to this.
I was able to get maven to re-render html when ever there is a change in index.adoc by changing <goal> from process-asciidoc to auto-refresh. However, this does not watch the change in the Test.
Question
Is there a way to tell Maven to watch our test files and re-compile when changes are made?
POM.XML
<plugin>
<groupId>org.asciidoctor</groupId>
<artifactId>asciidoctor-maven-plugin</artifactId>
<version>1.5.7.1</version>
<executions>
<execution>
<id>generate-docs</id>
<phase>prepare-package</phase>
<goals>
<goal>auto-refresh</goal>
</goals>
<configuration>
<sourceDocumentName>index.adoc</sourceDocumentName>
<backend>html</backend>
<attributes>
<snippets>${project.build.directory}/generated-snippets</snippets>
</attributes>
</configuration>
</execution>
</executions>
</plugin>
Thank you.
This is not a continuous build solution but it works similarly. However, the process does take some time because it essentially re-packages the project everytime there is a change... May not be ideal for some use cases...
I found a plugin that watches files. https://github.com/fizzed/maven-plugins
Change the watch directory to where your test files. Changed the goal from compile to package.
Watcher will execute mvnw: package when a change is detected. Then the asciidoctor maven plugin will re-package the project.
Add this to your plugin
<plugin>
<groupId>com.fizzed</groupId>
<artifactId>fizzed-watcher-maven-plugin</artifactId>
<version>1.0.6</version>
<configuration>
<touchFile>target/classes/watcher.txt</touchFile>
<watches>
<watch>
<directory><directory>src/test/[your test package]</directory></directory>
</watch>
</watches>
<goals>
<goal>package</goal>
<!-- <goal>compile</goal> -->
</goals>
<profiles>
<profile>optional-profile-to-activate</profile>
</profiles>
</configuration>
</plugin>
Maven does not have an equivalent of Gradle's continuous build. If you want changes in the tests to be detected and to trigger recompilation of the tests and execution of all of the tasks that depend (directly or indirectly) on the compiled test classes, you'll have to use Gradle.

export AspectJ project as jar and use in another application

I am writing an Aspect in Mavenised project. PROJECT_A
Now I want to use the PROJECT_A.jar in PROJECT_B which is non mavenised.
And in PROJECT_B i want to use the annotaition which I have written using the aspect in PROJECT_A .
I tried exporting PROJECT_A.jar - with aspectJ Plugin as follows. But when i use it in PROJECT_B - my annotation does not works as expected.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
I am new to the use of AspectJ and I am not getting how to do it without maven in PROJECT_B.
Please help , any help is appreciated :)
First of all, upgrade to AspectJ Maven 1.9.
Then mavenise project B! This enables you to you can use AspectJ Maven plugin to weave the aspects into your other module, even after compilation. This is called binary weaving. Please read the plugin documentation, especially the info about parameters aspectLibraries and weaveDependencies, depending on which approach you want to pursue.
Alternatively, start your JVM with -javaagent:/path/to/aspwctjweaver.jar and use the AspectJ weaving agent for load-time weaving (LTW). But do not forget then to also provide a LTW configuration via META-INF/aop.xml.
Either way, you have options. But both require you to read some documentation or at least search this site for examples.

Check war file before deploy / conditional deploy

When using cargo to deploy to a remote server I would like to check with cargo or maven that the generated war have it's properties files filtered like expected.
Some phase in between should test a property file from war against some strings and so deploy or stop deployment.
there's built in on cargo to achieve this?
Not sure what are the properties files you're referring to therefore I assume that you refer to typical java *.properties files.
Anyway, I believe you should use: maven-enforcer-plugin and it's goal: enforce as that is the common way in maven to enforce some condition (the plugin uses term: rule) to be fulfilled.
I think you have more options here.
Option 1
Maybe you could check that prio to packaging to your war using:
maven-property-plugin - goal: read-project-properties (http://mojo.codehaus.org/properties-maven-plugin/usage.html)
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<phase>???</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>your_file_to_check.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
where you should:
check the right phase to make sure the file is already filtered (http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html)
reference the file you want to check the properties of
And afterwards go for maven-enforcer-plugin goal: enforce and the rule: requireProperty (http://maven.apache.org/enforcer/enforcer-rules/requireProperty.html)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.3.1</version>
<executions>
<execution>
<id>enforce-property</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireProperty>
<property>your_property_to_check</property>
<message>You must set a your_property_to_check property!</message>
<regex>regex</regex>
<regexMessage>violation txt</regexMessage>
</requireProperty>
</rules>
<fail>true</fail>
</configuration>
</execution>
</executions>
</plugin>
where:
your_property_to_check should be replaced with the real one as well as
regex should be defined
Option 2
If that is not feasible and you want to check property inside war file, you might need to write your own rule.
That should not be a big deal as java has zip reading as well as properties files loading in it's standard API. To learn how to write custom rule, see: http://maven.apache.org/enforcer/enforcer-api/writing-a-custom-rule.html
Btw. I'd be quite curious to understand why would someone want to do check some property on each deployment? Is the case that your input (property files you filter) are dynamically generated/changed? Otherwise I doubt you need it once you check it works.

Spring-ws best way to generate java classes when autogeneration WSDL

I am autogenerating my wsdl using spring-ws and an XSD. Now I would like to generate java classes to return and do marschalling.
However I am seeing a lot of different ways to accomplish this, and not all are as clear about the benefit\detrement.
Some just save the generated WSDL in their project, others use their XSD file to generate, others add XJB etc...
My first thought was just adding:
<plugin>
<groupId>com.sun.tools.xjc.maven2</groupId>
<artifactId>maven-jaxb-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<removeOldOutput>true</removeOldOutput>
<schemaDirectory>src/main/webapp/WEB-INF</schemaDirectory>
</configuration>
</plugin>
to my pom.xml
My preferred way is jaxb2-maven-plugin. See http://mojo.codehaus.org/jaxb2-maven-plugin/usage.html
It's actually using XJC, a command that now comes with your current JDK (under bin in Windows or Commands in Mac)
You can call it directly with xjc -p your.package -wsdl your.wsdl
to use maven to generate java code from WSDL, you can check maven cxf codegen plugin wsdl to java.

How do I exclude a properties file when deploy

I want to include this file when running locally, but exclude it when deploy. I tried the following the doesn't seem to work.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<excludes>
<exclude>filename.properties</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
You should put that property file into src/test/resources than it will be available for Unit testing etc. but it will not being deployed.
I want to use this property file in development, but in production, I want to be able to use a different file.
There are several ways to achieve this but this is a perfect use case for profiles (and optionally filtering).
The Building For Different Environments with Maven 2 page explains how you could manage different property files and use the Maven Antrun plugin to pick up the "right" one depending on the profile used.
Instead of using the Antrun plugin, you could declare environment specific values as properties in different profiles in your POM and use Maven filtering. See Using Maven profiles and resource filtering for a full example. This is especially nice if you need to protect some informations (that you can "hide" in a profile inside ~/.m2/settings.xml). See also Tips and Tricks for an illustration of this.
Or, instead of putting properties inside profiles, you could put them in "filter files" and apply the right filter depending on the profile. This is a little variation of the above solution. See A Maven2 multi-environment filter setup for an example.