1. 2017-07-24 - Delombok Java Beans; Tags: Delombok Java Beans

    Delombok Java Beans

    If you are using Lombok you can let lombok generate via the Maven Plugin the source code.

    There are several approaches. Per se, Lombok expects a dedicated source folder src/main/lombok. If you have your Java Beans not in that folder, you can configure in the build section of the lombok maven plugin the sourceDirectory.

     <!-- delombok for source analysis-->

    If you are using the default, you need to add this source folder to the maven compiler plugin.

  2. 2016-11-08 - List dependency tree in Apache Maven; Tags: List dependency tree in Apache Maven

    List dependency tree in Apache Maven

    Do you ever run into the situation that a open source library uses a logging framework and you don’t know which one? If you use Apache Maven as build tool, the easiest solution is just to invoke mvn dependency:tree.

    It will give for instance this output:

    >[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ terminal-watcher --- [INFO] net.cinhtau.ao:terminal-watcher:jar:1.0-SNAPSHOT [INFO] +- org.elasticsearch.client:transport:jar:5.0.0:compile [INFO] | +- org.elasticsearch:elasticsearch:jar:5.0.0:compile [INFO] | | +- org.apache.lucene:lucene-core:jar:6.2.0:compile [INFO] | | +- org.apache.lucene:lucene-analyzers-common:jar:6.2.0:compile [INFO] | | +- org.apache.lucene:lucene-backward-codecs:jar:6.2.0:compile [INFO] | | +- org.apache.lucene:lucene-grouping:jar:6.2.0:compile [INFO] | | +- org.apache.lucene:lucene-highlighter:jar:6.2.0:compile [INFO] | | +- org.apache.lucene:lucene-join:jar:6.2.0:compile [INFO] | | +- org.apache.lucene:lucene-memory:jar:6.2.0:compile [INFO] | | +- org.apache.lucene:lucene-misc:jar:6.2.0:compile [INFO] | | +- org.apache.lucene:lucene-queries:jar:6.2.0:compile [INFO] | | +- org.apache.lucene:lucene-queryparser:jar:6.2.0:compile [INFO] | | +- org.apache.lucene:lucene-sandbox:jar:6.2.0:compile [INFO] | | +- org.apache.lucene:lucene-spatial:jar:6.2.0:compile [INFO] | | +- org.apache.lucene:lucene-spatial-extras:jar:6.2.0:compile [INFO] | | +- org.apache.lucene:lucene-spatial3d:jar:6.2.0:compile [INFO] | | +- org.apache.lucene:lucene-suggest:jar:6.2.0:compile [INFO] | | +- org.elasticsearch:securesm:jar:1.1:compile [INFO] | | +- net.sf.jopt-simple:jopt-simple:jar:5.0.2:compile [INFO] | | +- com.carrotsearch:hppc:jar:0.7.1:compile [INFO] | | +- joda-time:joda-time:jar:2.9.4:compile [INFO] | | +- org.joda:joda-convert:jar:1.2:compile [INFO] | | +- org.yaml:snakeyaml:jar:1.15:compile [INFO] | | +- com.fasterxml.jackson.core:jackson-core:jar:2.8.1:compile [INFO] | | +- com.fasterxml.jackson.dataformat:jackson-dataformat-smile:jar:2.8.1:compile [INFO] | | +- com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:jar:2.8.1:compile [INFO] | | +- com.fasterxml.jackson.dataformat:jackson-dataformat-cbor:jar:2.8.1:compile [INFO] | | +- com.tdunning:t-digest:jar:3.0:compile [INFO] | | +- org.hdrhistogram:HdrHistogram:jar:2.1.6:compile [INFO] | | \- net.java.dev.jna:jna:jar:4.2.2:compile [INFO] | +- org.elasticsearch.plugin:transport-netty3-client:jar:5.0.0:compile [INFO] | | \- io.netty:netty:jar:3.10.6.Final:compile [INFO] | +- org.elasticsearch.plugin:transport-netty4-client:jar:5.0.0:compile [INFO] | | +- io.netty:netty-buffer:jar:4.1.5.Final:compile [INFO] | | +- io.netty:netty-codec:jar:4.1.5.Final:compile [INFO] | | +- io.netty:netty-codec-http:jar:4.1.5.Final:compile [INFO] | | +- io.netty:netty-common:jar:4.1.5.Final:compile [INFO] | | +- io.netty:netty-handler:jar:4.1.5.Final:compile [INFO] | | +- io.netty:netty-resolver:jar:4.1.5.Final:compile [INFO] | | \- io.netty:netty-transport:jar:4.1.5.Final:compile [INFO] | +- org.elasticsearch.plugin:reindex-client:jar:5.0.0:compile [INFO] | | \- org.elasticsearch.client:rest:jar:5.0.0:compile [INFO] | | +- org.apache.httpcomponents:httpclient:jar:4.5.2:compile [INFO] | | +- org.apache.httpcomponents:httpcore:jar:4.4.5:compile [INFO] | | +- org.apache.httpcomponents:httpasyncclient:jar:4.1.2:compile [INFO] | | +- org.apache.httpcomponents:httpcore-nio:jar:4.4.5:compile [INFO] | | +- commons-codec:commons-codec:jar:1.10:compile [INFO] | | \- commons-logging:commons-logging:jar:1.1.3:compile [INFO] | +- org.elasticsearch.plugin:lang-mustache-client:jar:5.0.0:compile [INFO] | | \- com.github.spullara.mustache.java:compiler:jar:0.9.3:compile [INFO] | \- org.elasticsearch.plugin:percolator-client:jar:5.0.0:compile [INFO] +- org.slf4j:slf4j-api:jar:1.7.21:compile [INFO] +- org.apache.logging.log4j:log4j-slf4j-impl:jar:2.7:compile [INFO] | \- org.apache.logging.log4j:log4j-api:jar:2.7:compile [INFO] \- org.apache.logging.log4j:log4j-core:jar:2.7:compile [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 8.050 s [INFO] Finished at: 2016-11-04T17:03:45+01:00 [INFO] Final Memory: 18M/211M [INFO] ------------------------------------------------------------------------

    You can see that log4j version 2.7 is used :wink: .

  3. 2015-10-01 - Build single module from multi module pom; Tags: Build single module from multi module pom

    Build single module from multi module pom

    If case you want to test or build only a project (sub module) you have several options since Maven 2.1

    This example maven project has sub module which generates an rpm package.

    |-- api
    |-- impl
    |-- db
    |-- ear
    |-- deploy
        |-- rpm
        |-- deb
        |-- osgi

    Invoking the maven goal rpm:rpm applies only to the project rpm. Invoking on path coolapp

    mvn rpm:rpm --projects deploy/rpm
    # short form
    mvn rpm:rpm -pl deploy/rpm

    If you want to also build dependencies

    mvn rpm:rpm --projects --also-make deploy/rpm
    # short form
    mvn rpm:rpm -pl -am deploy/rpm

    Take also a look at advanced reactor options.

  4. 2015-08-31 - Walking the file tree with Java 7 NIO 2; Tags: Walking the file tree with Java 7 NIO 2

    Walking the file tree with Java 7 NIO 2

    Non-blocking I/O (usually called NIO, and sometimes called “New I/O”) is a collection of Java programming language APIs that offer features for intensive I/O operations.

    To walk a file tree, you first need to implement a FileVisitor. A FileVisitor specifies the required behavior at key points in the traversal process: when a file is visited, before a directory is accessed, after a directory is accessed, or when a failure occurs.

    See The Java™ Tutorials

    Following JUnit test example validates all XML files in a Maven resources directory with the Java built-in SAX (Simple API for XML) API.

     * Validates all the xml docs in `src/main/resources`
    public class XmlValdationTest {
        private static final Logger LOGGER = LoggerFactory.getLogger(XmlValdationTest.class);
        private static final String XSD_FILENAME = XSD.getFileName();
        private Set<String> excludeFiles;
        private javax.xml.validation.Validator validator;
        public void setUp() throws SAXException {
            // setup file excludes
            // prepare Validator
            Source xmlFile = new StreamSource(retrieveSchema());
            SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            Schema schema = schemaFactory.newSchema(xmlFile);
            this.validator = schema.newValidator();
        /** add files to exclude here */
        private void setUpExcludes() {
            this.excludeFiles = new HashSet<String>();
         * retrieve current xsd from artifact xmlschema
         * @return
        private InputStream retrieveSchema() {
            return ClassLoader.getSystemResourceAsStream(XSD_FILENAME);
        public void testValidation() throws IOException {
            // arrange - get files
            Path folder = Paths.get("src", "main", "resources", "xml");
            // act
        private void validate(Path folder) throws IOException {
            final List<Path> xmlFiles = new ArrayList<>();
            Files.walkFileTree(folder, new SimpleFileVisitor<Path>() {
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    if (file.getFileName().toString().endsWith(".xml") && (!excludeFiles.contains(file.getFileName().toString()))) {
                    return FileVisitResult.CONTINUE;
            for (Path path : xmlFiles) {
                try {
                    // act
                    this.validator.validate(new StreamSource(path.toFile()));
                    LOGGER.info("{} is valid", path.getFileName());
                } catch (SAXException e) {
                    // assert
                    LOGGER.error("Validation failed for {}", path.getFileName(), e);
                    Assert.fail(String.format("%s is NOT valid", path.getFileName()));
  5. 2015-08-24 - Validate Apache Maven SCM settings; Tags: Validate Apache Maven SCM settings

    Validate Apache Maven SCM settings

    To perform a release of your Maven Artifact, the pom.xml needs a SCM section. To validate or check if the SCM settings are correct, you can use the Maven goal:

    mvn scm:validate

    Maven supports various SCM systems like Subversion (SVN), git or Mercurial (hg). See a current matrix which SCM systems and features are supported by the Maven release plugin.

  6. 2015-07-06 - Testing with Maven; Tags: Testing with Maven

    Testing with Maven

    The Maven target test executes all tests under src/test. This article assumes you are familiar with JUnit tests.

    Skip tests

    Maven comes with an option to skip the unit test, -Dmaven.test.skip=true. If you absolutely must, you can also use the maven.test.skip property to skip compiling the tests. maven.test.skip is honored by Surefire, Failsafe and the Compiler Plugin.

    mvn install -Dmaven.test.skip=true
    mvn package -Dmaven.test.skip=true

    You can also skip the tests via command line by executing the following command:

    mvn install -DskipTests

    Skip integration tests

    Since skipTests is also followed by the Surefire Plugin, this will have the effect of not running any tests. If, instead, you want to skip only the integration tests being run by the Failsafe Plugin, you would use the skipITs property

    mvn install -DskipITs

    Running a Single Test

    «During development, you may run a single test class repeatedly. To run this through Maven, set the test property to a specific test case.»http://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html

    mvn -Dtest=TestCircle test

    The value for the test parameter is the name of the test class (without the extension; we’ll strip off the extension if you accidentally provide one).

    You may also use patterns to run a number of tests:

    mvn -Dtest=TestCi*le test

    And you may use multiple names/patterns, separated by commas:

    mvn -Dtest=TestSquare,TestCi*le test

    Running a Set of Methods in a Single Test Class

    As of Surefire 2.7.3, you can also run only a subset of the tests in a test class. NOTE : This feature is supported only for Junit 4.x and TestNG. You must use the following syntax:

    mvn -Dtest=TestCircle#mytest test

    You can use patterns too

    mvn -Dtest=TestCircle#test* test

    As of Surefire 2.12.1, you can select multiple methods

    mvn -Dtest=TestCircle#testOne+testTwo test
  7. 2015-06-23 - Purge maven dependencies from local repository; Tags: Purge maven dependencies from local repository

    Purge maven dependencies from local repository

    The Apache Maven dependency plugin provides the function to remove projects dependencies from the local repository.

    The command has to be run in the maven project where the pom.xml resides.

    mvn dependency:purge-local-repository -Dmaven.repo.local=/path/to/repo

    If you have a lot of snapshot versions, you can remove than completely and download the latest snapshot from your maven repository.