Wednesday, December 7, 2022

Maven assembly plugin

 HOWTO

Project input 

  • Java sources
  • resources files
  • 3rd party libraries for compilation only

Project output 

  • packaged JARs
  • packaged EARs
  • packaged WARs
  • config files
  • 3rd party libraries required for runtime
  • complete TAR file delivering the application

Artifact types

JAR

  • Java classes
  • descriptors

EAR

  • JARs - dependencies
  • descriptors

WAR 

  • JARs - dependencies 
  • Web code
  •  descriptors

 

 Test project

Sample assembly descriptor

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">

    <id>package-artifacts</id>
    <!-- Final distributive format. -->
    <formats>
        <format>tar</format>
    </formats>

    <!-- Exclude current module from distributive. -->
    <includeBaseDirectory>false</includeBaseDirectory>

    <!-- Project files to include to distributive.
    ${maven.multiModuleProjectDirectory} - path to root project folder. -->
    <fileSets>
        <fileSet>
            <!-- From directory. -->
        <directory>${maven.multiModuleProjectDirectory}/add-to-distribution/docs</directory>
            <!-- Directory in distributive. -->
            <outputDirectory>docs</outputDirectory>
            <includes>
                <!-- ** - include all files of any nesting. -->
                <include>**</include>
            </includes>
        </fileSet>
    </fileSets>

    <!-- Set of modules, included in distributive. -->
    <dependencySets>
        <dependencySet>
            <includes>
                <!-- Include all modules from POM-dependencies current module. -->
                <include>*</include>
            </includes>
            <!-- Target folder for modules in distributive. -->
            <outputDirectory>modules</outputDirectory>
            <useProjectArtifact>true</useProjectArtifact>
            <useTransitiveDependencies>false</useTransitiveDependencies>
        </dependencySet>
    </dependencySets>

</assembly>

Run mvn install

INFO] -------------------------< dave:dist-assembly >-------------------------
[INFO] Building dist-assembly 1.0.0-SNAPSHOT                              [3/3]
[INFO] --------------------------------[ pom ]---------------------------------
[INFO] 
[INFO] --- maven-assembly-plugin:2.2-beta-5:single (create-dist) @ dist-assembly ---
[INFO] Reading assembly descriptor: dist-assembly.xml
[WARNING] Cannot include project artifact: dave:dist-assembly:pom:1.0.0-SNAPSHOT; it doesn't have an associated file or directory.
[WARNING] The following patterns were never triggered in this artifact inclusion filter:
o  '*'

[INFO] Building tar : /git/java-tutorial/assembly/target/dist-assembly-1.0.0-SNAPSHOT-package-artifacts.tar
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ dist-assembly ---
[INFO] Installing /git/java-tutorial/assembly/pom.xml to /home/dave/.m2/repository/dave/dist-assembly/1.0.0-SNAPSHOT/dist-assembly-1.0.0-SNAPSHOT.pom
[INFO] Installing /git/java-tutorial/assembly/target/dist-assembly-1.0.0-SNAPSHOT-package-artifacts.tar to /home/dave/.m2/repository/dave/dist-assembly/1.0.0-SNAPSHOT/dist-assembly-1.0.0-SNAPSHOT-package-artifacts.tar
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for parent 1.0.0-SNAPSHOT:
[INFO] 
[INFO] parent ............................................. SUCCESS [  0.208 s]
[INFO] common ............................................. SUCCESS [  1.436 s]
[INFO] dist-assembly ...................................... SUCCESS [  0.301 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.049 s
[INFO] Finished at: 2022-12-08T20:50:54+01:00
[INFO] ------------------------------------------------------------------------
[dave@dave java-tutorial]$ tar tvf /git/java-tutorial/assembly/target/dist-1.0.0-SNAPSHOT-package-artifacts.tar
-rw-r--r-- dave/dave      5181 2022-12-08 20:47 modules/common-1.0.0-SNAPSHOT.jar
drwxr-xr-x dave/dave         0 2022-12-08 20:35 docs/
-rw-r--r-- dave/dave        12 2022-12-08 20:35 docs/file-to-add.txt

Project structure

./common
./common/src
./common/src/main
./common/src/main/java
./common/src/main/java/gc
./common/src/main/java/gc/LogGC.java
./common/src/main/java/generics
./common/src/main/java/generics/AbstractValue.java
./common/src/main/java/generics/BoxInteger.java
./common/src/main/java/generics/BoxString.java
./common/src/main/java/generics/GenericsTutorial.java
./common/src/main/java/ldap
./common/src/main/java/ldap/TestLDAP.java
./common/pom.xml
./assembly
./assembly/dist-assembly.xml
./assembly/pom.xml
./add-to-distribution
./add-to-distribution/docs
./add-to-distribution/docs/file-to-add.txt
./pom.xml


Sample code

 ear

      <plugin>
        <artifactId>maven-ear-plugin</artifactId>
        <version>3.3.0</version>
        <configuration>
          <!-- configuration elements goes here -->
        </configuration>
      </plugin>

Packaged file name

${project.build.directory}/${project.build.finalName}.${project.packaging}

Copy all project dependencies to target/lib

<plugin>
   <artifactId>maven-dependency-plugin</artifactId>
   <executions>
      <execution>
         <phase>package</phase>
         <goals>
             <goal>copy-dependencies</goal>
         </goals>
             <configuration>
               <outputDirectory>${project.build.directory}/lib</outputDirectory>
             </configuration>
       </execution>
    </executions>
</plugin>

Exclude transitive dependencies 

  <dependency>
            <groupId>com.dave</groupId>
            <artifactId>dave-module</artifactId>
            <version>1.0.0</version>
            <exclusions>
                <exclusion>
                    <groupId>*</groupId>
                    <artifactId>*</artifactId>
                </exclusion>
            </exclusions>
</dependency>

 war

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.3.2</version>
        <configuration>
          <webappDirectory>/sample/servlet/container/deploy/directory</webappDirectory>
        </configuration>
      </plugin>
    </plugins>
  </build>

assembly 

Exclude artifacts from dependency set

      <dependencySets>
        <dependencySet>
          ....
          <excludes>
            <exclude>commons-lang:commons-lang</exclude>
            <exclude>log4j:log4j</exclude>
          </excludes>
        </dependencySet>
        ....
      </dependencySets>

Full assembly descriptor

    <assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.1 https://maven.apache.org/xsd/assembly-2.1.1.xsd">
      <id/>
      <formats/>
      <includeBaseDirectory/>
      <baseDirectory/>
      <includeSiteDirectory/>
      <containerDescriptorHandlers>
        <containerDescriptorHandler>
          <handlerName/>
          <configuration/>
        </containerDescriptorHandler>
      </containerDescriptorHandlers>
      <moduleSets>
        <moduleSet>
          <useAllReactorProjects/>
          <includeSubModules/>
          <includes/>
          <excludes/>
          <sources>
            <useDefaultExcludes/>
            <outputDirectory/>
            <includes/>
            <excludes/>
            <fileMode/>
            <directoryMode/>
            <fileSets>
              <fileSet>
                <useDefaultExcludes/>
                <outputDirectory/>
                <includes/>
                <excludes/>
                <fileMode/>
                <directoryMode/>
                <directory/>
                <lineEnding/>
                <filtered/>
                <nonFilteredFileExtensions/>
              </fileSet>
            </fileSets>
            <includeModuleDirectory/>
            <excludeSubModuleDirectories/>
            <outputDirectoryMapping/>
          </sources>
          <binaries>
            <outputDirectory/>
            <includes/>
            <excludes/>
            <fileMode/>
            <directoryMode/>
            <attachmentClassifier/>
            <includeDependencies/>
            <dependencySets>
              <dependencySet>
                <outputDirectory/>
                <includes/>
                <excludes/>
                <fileMode/>
                <directoryMode/>
                <useStrictFiltering/>
                <outputFileNameMapping/>
                <unpack/>
                <unpackOptions>
                  <includes/>
                  <excludes/>
                  <filtered/>
                  <nonFilteredFileExtensions/>
                  <lineEnding/>
                  <useDefaultExcludes/>
                  <encoding/>
                </unpackOptions>
                <scope/>
                <useProjectArtifact/>
                <useProjectAttachments/>
                <useTransitiveDependencies/>
                <useTransitiveFiltering/>
              </dependencySet>
            </dependencySets>
            <unpack/>
            <unpackOptions>
              <includes/>
              <excludes/>
              <filtered/>
              <nonFilteredFileExtensions/>
              <lineEnding/>
              <useDefaultExcludes/>
              <encoding/>
            </unpackOptions>
            <outputFileNameMapping/>
          </binaries>
        </moduleSet>
      </moduleSets>
      <fileSets>
        <fileSet>
          <useDefaultExcludes/>
          <outputDirectory/>
          <includes/>
          <excludes/>
          <fileMode/>
          <directoryMode/>
          <directory/>
          <lineEnding/>
          <filtered/>
          <nonFilteredFileExtensions/>
        </fileSet>
      </fileSets>
      <files>
        <file>
          <source/>
          <sources/>
          <outputDirectory/>
          <destName/>
          <fileMode/>
          <lineEnding/>
          <filtered/>
        </file>
      </files>
      <dependencySets>
        <dependencySet>
          <outputDirectory/>
          <includes/>
          <excludes/>
          <fileMode/>
          <directoryMode/>
          <useStrictFiltering/>
          <outputFileNameMapping/>
          <unpack/>
          <unpackOptions>
            <includes/>
            <excludes/>
            <filtered/>
            <nonFilteredFileExtensions/>
            <lineEnding/>
            <useDefaultExcludes/>
            <encoding/>
          </unpackOptions>
          <scope/>
          <useProjectArtifact/>
          <useProjectAttachments/>
          <useTransitiveDependencies/>
          <useTransitiveFiltering/>
        </dependencySet>
      </dependencySets>
      <repositories>
        <repository>
          <outputDirectory/>
          <includes/>
          <excludes/>
          <fileMode/>
          <directoryMode/>
          <includeMetadata/>
          <groupVersionAlignments>
            <groupVersionAlignment>
              <id/>
              <version/>
              <excludes/>
            </groupVersionAlignment>
          </groupVersionAlignments>
          <scope/>
        </repository>
      </repositories>
      <componentDescriptors/>
    </assembly>

Saturday, December 3, 2022

Calling Ant from Maven using maven-antrun-plugin

 HOWTO

Sample code

            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    <execution>
                        <phase>
                            package
                        </phase>
                        <configuration>
                            <target>
                                <echoproperties />
                                <property name="compile_classpath" refid="maven.compile.classpath" />
                                <property name="runtime_classpath" refid="maven.runtime.classpath" />
                                <property name="test_classpath" refid="maven.test.classpath" />
                                <property name="plugin_classpath" refid="maven.plugin.classpath" />
                                 
                                <echo message="compile classpath: ${compile_classpath}" />
                                <echo message="runtime classpath: ${runtime_classpath}" />
                                <echo message="test classpath:    ${test_classpath}" />
                                <echo message="plugin classpath:  ${plugin_classpath}" />
                            </target>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

Ant packaged JAR installed into Maven repository

Code

<jar destfile="${project.build.directory}/ant-generated.jar" 
      basedir="${project.build.directory}/classes"
      includes="$(project.build.directory}//classes**/*.class" 
      excludes="**/Test.class"
/>

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-install-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <groupId>dave</groupId>
                    <artifactId>ant-jar</artifactId>
                    <version>1.0</version>
                    <packaging>jar</packaging>
                    <file>target/ant-generated.jar</file>
                    <generatePom>true</generatePom>
                </configuration>
                <executions>
                    <execution>
                        <id>install-jar-lib</id>
                        <goals>
                            <goal>install-file</goal>
                        </goals>
                        <phase>package</phase>
                    </execution>
                </executions>
</plugin>

Output

INFO]       [jar] Building MANIFEST-only jar: /git/java-tutorial/common/target/ant-generated.jar
[INFO] Executed tasks
[INFO] 
[INFO] --- maven-install-plugin:2.5.1:install-file (install-jar-lib) @ tutorial ---
[INFO] pom.xml not found in ant-generated.jar
[INFO] Installing /git/java-tutorial/common/target/ant-generated.jar to /home/dave/.m2/repository/dave/ant-jar/1.0/ant-jar-1.0.jar
[INFO] Installing /tmp/mvninstall5653662900106884773.pom to /home/dave/.m2/repository/dave/ant-jar/1.0/ant-jar-1.0.pom

Special Maven properties exported to Ant


maven.multiModuleProjectDirectory=/git/java-tutorial
project.build.directory=/git/java-tutorial/common/target

Maven classpath exported into Ant

[WARNING]      [echo] compile classpath: /git/java-tutorial/common/target/classes
[WARNING]      [echo] runtime classpath: /git/java-tutorial/common/target/classes
[WARNING]      [echo] test classpath:    /git/java-tutorial/common/target/test-classes:/git/java-tutorial/common/target/classes:/home/dave/.m2/repository/junit/junit/4.11/junit-4.11.jar:/home/dave/.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar
[WARNING]      [echo] plugin classpath:  /home/dave/.m2/repository/org/apache/maven/plugins/maven-antrun-plugin/3.1.0/maven-antrun-plugin-3.1.0.jar:/home/dave/.m2/repository/org/codehaus/plexus/plexus-utils/3.4.1/plexus-utils-3.4.1.jar:/home/dave/.m2/repository/org/apache/ant/ant/1.10.12/ant-1.10.12.jar:/home/dave/.m2/repository/org/apache/ant/ant-launcher/1.10.12/ant-launcher-1.10.12.jar
                      

Exporting properties from Maven to ant 

 See https://maven.apache.org/plugins/maven-antrun-plugin/usage.html 

If the Maven property you want to use is not available in an external file, you will have to redefine the property before calling ant.

      <property name="maven.project.url" value="${project.url}"/>
      <ant antfile="build.xml"/>

 Ant  properties defined in  maven-antrun-plugin call

FO] --- maven-antrun-plugin:3.1.0:run (default) @ tutorial ---
[INFO] Executing tasks
[INFO] [echoproperties] #Ant properties
[INFO] [echoproperties] #Sat Dec 03 19:41:56 CET 2022
[INFO] [echoproperties] ant.file.maven-antrun-=/git/java-tutorial/common/target/antrun/build-main.xml
[INFO] [echoproperties] sun.desktop=gnome
[INFO] [echoproperties] awt.toolkit=sun.awt.X11.XToolkit
[INFO] [echoproperties] project.build.directory=/git/java-tutorial/common/target
[INFO] [echoproperties] java.specification.version=11
[INFO] [echoproperties] failOnMissingWebXml=false
[INFO] [echoproperties] ant.project.name=maven-antrun-
[INFO] [echoproperties] sun.cpu.isalist=
[INFO] [echoproperties] sun.jnu.encoding=UTF-8
[INFO] [echoproperties] git.build.version=0.0.1-SNAPSHOT
[INFO] [echoproperties] java.class.path=/app/weblogic/oracle_common/modules/thirdparty/apache-maven_bundle/3.6.1.0.0/apache-maven-3.6.1/boot/plexus-classworlds-2.6.0.jar
[INFO] [echoproperties] project.name=tutorial
[INFO] [echoproperties] project.build.testOutputDirectory=/git/java-tutorial/common/target/test-classes
[INFO] [echoproperties] java.vm.vendor=Oracle Corporation
[INFO] [echoproperties] sun.arch.data.model=64
[INFO] [echoproperties] settings.localRepository=/home/dave/.m2/repository
[INFO] [echoproperties] project.groupId=dave
[INFO] [echoproperties] java.vendor.url=https\://openjdk.java.net/
[INFO] [echoproperties] user.timezone=Europe/Prague
[INFO] [echoproperties] project.build.outputDirectory=/git/java-tutorial/common/target/classes
[INFO] [echoproperties] maven.conf=/app/weblogic/oracle_common/modules/thirdparty/apache-maven_bundle/3.6.1.0.0/apache-maven-3.6.1/conf
[INFO] [echoproperties] java.vm.specification.version=11
[INFO] [echoproperties] os.name=Linux
[INFO] [echoproperties] ant.file.type.maven-antrun-=file
[INFO] [echoproperties] user.country=US
[INFO] [echoproperties] sun.java.launcher=SUN_STANDARD
[INFO] [echoproperties] sun.boot.library.path=/usr/java/jdk-11.0.16.1/lib
[INFO] [echoproperties] sun.java.command=org.codehaus.plexus.classworlds.launcher.Launcher clean package
[INFO] [echoproperties] jdk.debug=release
[INFO] [echoproperties] maven.home=/app/weblogic/oracle_common/modules/thirdparty/apache-maven_bundle/3.6.1.0.0/apache-maven-3.6.1
[INFO] [echoproperties] sun.cpu.endian=little
[INFO] [echoproperties] maven.compiler.source=11
[INFO] [echoproperties] user.home=/home/dave
[INFO] [echoproperties] project.build.testSourceDirectory=/git/java-tutorial/common/src/test/java
[INFO] [echoproperties] user.language=en
[INFO] [echoproperties] org.hamcrest\:hamcrest-core\:jar=/home/dave/.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar
[INFO] [echoproperties] java.specification.vendor=Oracle Corporation
[INFO] [echoproperties] project.artifactId=tutorial
[INFO] [echoproperties] project.version=0.0.1-SNAPSHOT
[INFO] [echoproperties] java.version.date=2022-08-18
[INFO] [echoproperties] java.home=/usr/java/jdk-11.0.16.1
[INFO] [echoproperties] basedir=/git/java-tutorial/common
[INFO] [echoproperties] file.separator=/
[INFO] [echoproperties] project.packaging=jar
[INFO] [echoproperties] java.vm.compressedOopsMode=Zero based
[INFO] [echoproperties] line.separator=\n
[INFO] [echoproperties] ant.java.version=11
[INFO] [echoproperties] java.specification.name=Java Platform API Specification
[INFO] [echoproperties] java.vm.specification.vendor=Oracle Corporation
[INFO] [echoproperties] java.awt.graphicsenv=sun.awt.X11GraphicsEnvironment
[INFO] [echoproperties] git.commit.id.full=ff3ab46b0d66dfa9c433058be438f2cb0c2ea3b1
[INFO] [echoproperties] git.build.time=2022-12-03T19\:41\:55+0100
[INFO] [echoproperties] sun.management.compiler=HotSpot 64-Bit Tiered Compilers
[INFO] [echoproperties] java.runtime.version=11.0.16.1+1-LTS-1
[INFO] [echoproperties] user.name=dave
[INFO] [echoproperties] ant.file=/git/java-tutorial/common/pom.xml
[INFO] [echoproperties] path.separator=\:
[INFO] [echoproperties] securerandom.source=file\:/dev/./urandom
[INFO] [echoproperties] os.version=6.0.10-200.fc36.x86_64
[INFO] [echoproperties] java.runtime.name=Java(TM) SE Runtime Environment
[INFO] [echoproperties] file.encoding=UTF-8
[INFO] [echoproperties] guice.disable.misplaced.annotation.check=true
[INFO] [echoproperties] java.vm.name=Java HotSpot(TM) 64-Bit Server VM
[INFO] [echoproperties] java.vendor.version=18.9
[INFO] [echoproperties] localRepository=\      id\: local\n      url\: file\:///home/dave/.m2/repository/\n   layout\: default\nsnapshots\: [enabled \=> true, update \=> always]\n releases\: [enabled \=> true, update \=> always]\n
[INFO] [echoproperties] ant.core.lib=/home/dave/.m2/repository/org/apache/ant/ant/1.10.12/ant-1.10.12.jar
[INFO] [echoproperties] java.vendor.url.bug=https\://bugreport.java.com/bugreport/
[INFO] [echoproperties] java.io.tmpdir=/tmp
[INFO] [echoproperties] java.version=11.0.16.1
[INFO] [echoproperties] user.dir=/git/java-tutorial
[INFO] [echoproperties] os.arch=amd64
[INFO] [echoproperties] maven.multiModuleProjectDirectory=/git/java-tutorial
[INFO] [echoproperties] git.commit.id.abbrev=ff3ab46
[INFO] [echoproperties] java.vm.specification.name=Java Virtual Machine Specification
[INFO] [echoproperties] java.awt.printerjob=sun.print.PSPrinterJob
[INFO] [echoproperties] sun.os.patch.level=unknown
[INFO] [echoproperties] ant.version=Apache Ant(TM) version 1.10.12 compiled on October 13 2021
[INFO] [echoproperties] maven.compiler.target=11
[INFO] [echoproperties] java.library.path=/usr/java/packages/lib\:/usr/lib64\:/lib64\:/lib\:/usr/lib
[INFO] [echoproperties] project.build.sourceDirectory=/git/java-tutorial/common/src/main/java
[INFO] [echoproperties] java.vm.info=mixed mode
[INFO] [echoproperties] java.vendor=Oracle Corporation
[INFO] [echoproperties] java.vm.version=11.0.16.1+1-LTS-1
[INFO] [echoproperties] classworlds.conf=/app/weblogic/oracle_common/modules/thirdparty/apache-maven_bundle/3.6.1.0.0/apache-maven-3.6.1/bin/m2.conf
[INFO] [echoproperties] sun.io.unicode.encoding=UnicodeLittle
[INFO] [echoproperties] library.jansi.path=/app/weblogic/oracle_common/modules/thirdparty/apache-maven_bundle/3.6.1.0.0/apache-maven-3.6.1/lib/jansi-native
[INFO] [echoproperties] maven.project.dependencies.versions=4.11\:1.3\:
[INFO] [echoproperties] java.class.version=55.0
[INFO] [echoproperties] junit\:junit\:jar=/home/dave/.m2/repository/junit/junit/4.11/junit-4.11.jar
[INFO] [echoproperties] ant.project.default-target=main
[WARNING]      [echo] compile classpath: /git/java-tutorial/common/target/classes
[WARNING]      [echo] runtime classpath: /git/java-tutorial/common/target/classes
[WARNING]      [echo] test classpath:    /git/java-tutorial/common/target/test-classes:/git/java-tutorial/common/target/classes:/home/dave/.m2/repository/junit/junit/4.11/junit-4.11.jar:/home/dave/.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar
[WARNING]      [echo] plugin classpath:  /home/dave/.m2/repository/org/apache/maven/plugins/maven-antrun-plugin/3.1.0/maven-antrun-plugin-3.1.0.jar:/home/dave/.m2/repository/org/codehaus/plexus/plexus-utils/3.4.1/plexus-utils-3.4.1.jar:/home/dave/.m2/repository/org/apache/ant/ant/1.10.12/ant-1.10.12.jar:/home/dave/.m2/repository/org/apache/ant/ant-launcher/1.10.12/ant-launcher-1.10.12.jar

Monday, November 28, 2022

Maven helper plugin - add sources, compile plugin - include/exclude sources, JAR plugin - include/exclude classes, create additional JARs from module with classifier

 HOWTO

Control what is compiled in Maven module - non-standard configuration

Input

  • Add other Java sources 
  • Add other resource files
  • Filter sources

Output 

  •  Filter classes  added into JAR
  •  Create additional JARs from compiled sources

buider-helper-maven-plugin configuration

add sources from other modules
<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>3.3.0</version>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>add-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>src/main/generated</source>
                        </sources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

maven-compile-plugin configuration 

  • define different main source dir
  • add sources from other modules
<build>
    <sourceDirectory>../moduleB</sourceDirectory>
    <plugins>
        <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.10.1</version>
        <configuration>
        <includes>
            <include>../moduleC/src/main/java/**/*.java</include>
            <include>../moduleD/src/main/java/**/*.java</include>
        </includes>
        </configuration>
        </plugin>
    </plugins>
</build>

maven-resources-plugin

  • add additional files to resources to be included in package
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>3.3.0</version>
        <executions>
          <execution>
            <id>copy-resources</id>
            <!-- here the phase you need -->
            <phase>validate</phase>
            <goals>
              <goal>copy-resources</goal>
            </goals>
            <configuration>
              <outputDirectory>${basedir}/target/extra-resources</outputDirectory>
              <resources>          
                <resource>
                  <directory>src/non-packaged-resources</directory>
                  <filtering>true</filtering>
                </resource>
              </resources>              
            </configuration>            
          </execution>
        </executions>
      </plugin>


maven-jar-plugin configuration 

  • - include/exclude classes from module JAR
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.3.0</version>
                <configuration>
                    <includes>
                        <include>**/generics/*</include>
                    </includes>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                        <configuration>
                            <classifier>test-ldap</classifier>
                            <includes>
                                <include>**/ldap/*</include>
                            </includes>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

JARs created

[INFO] --- maven-jar-plugin:3.3.0:jar (default-jar) @ tutorial ---
[INFO] Building jar: /git/java-tutorial/target/tutorial.jar
[INFO] 
[INFO] --- maven-jar-plugin:3.3.0:jar (default) @ tutorial ---
[INFO] Building jar: /git/java-tutorial/target/tutorial-test-ldap.jar

Sunday, November 20, 2022

Install SSL proxy intercept certificate for Azure CLI on Ubuntu

HOWTO

 

Add SSL intercept certificate to local OS storage

sudo cp SSL-intercept.crt /usr/local/share/ca-certificates/
sudo update-ca-certificates
export REQUESTS_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt

Add into .bashrc

export REQUESTS_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt

Add SSL intercept certificate to Python

cp SSL-intercept.crt  /usr/lib/python3/dist-packages/certifi/

Thursday, October 20, 2022

Docker UBI RedHat image

HOWTO 

 

Search for image

Pastdave@dave ~]$ docker search registry.access.redhat.com/ubi
NAME                      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
ubi8/go-toolset           Platform for building and running Go 1.11.5 …   0                    
ubi9/go-toolset           rhcc_registry.access.redhat.com_ubi9/go-tool…   0                    
ubi7                      The Universal Base Image is designed and eng…   0                    
ubi9-beta/ubi             Provides the latest release of Red Hat Unive…   0                    
ubi8/openjdk-8-runtime    OpenJDK 1.8 runtime-only image on Red Hat Un…   0                    
ubi8/openjdk-11-runtime   OpenJDK 11 runtime-only image on Red Hat Uni…   0                    
ubi8/openjdk-17-runtime   OpenJDK 17 runtime-only image on Red Hat Uni…   0                    
ubi7/ubi-minimal          The Universal Base Image Init is designed to…   0                    
ubi7/ubi-init             The Universal Base Image Init is designed to…   0                    
ubi8-minimal              The Universal Base Image Minimal is a stripp…   0                    
ubi7-minimal              The Universal Base Image Minimal is a stripp…   0                    
ubi8/ubi-micro            Provides the latest release of Micro Univers…   0                    
ubi9-beta/ubi-minimal     Provides the latest release of the Minimal R…   0                    
ubi9/ubi-init             rhcc_registry.access.redhat.com_ubi9/ubi-init   0                    
ubi9-init                 rhcc_registry.access.redhat.com_ubi9-init       0                    
ubi9-micro                rhcc_registry.access.redhat.com_ubi9-micro      0                    
ubi7/ubi                  The Universal Base Image is designed and eng…   0                    
ubi8/ubi                  Provides the latest release of the Red Hat U…   0                    
ubi8                      The Universal Base Image is designed and eng…   0                    
ubi9/ubi                  rhcc_registry.access.redhat.com_ubi9/ubi        0                    
ubi9                      rhcc_registry.access.redhat.com_ubi9            0                    
ubi8/ubi-minimal          Provides the latest release of the Minimal R…   0                    
ubi8/ubi-init             Provides the latest release of the Red Hat U…   0                    
ubi8-init                 The Universal Base Image Init is designed to…   0                    
ubi7-init                 The Universal Base Image Init is designed to…   0                    
ubi8-micro                Provides the latest release of Micro Univers…   0                    
ubi9-beta/ubi-init        Provides the latest release of the Red Hat U…   0                    
ubi9-beta/ubi-micro       Provides the latest release of Micro Univers…   0                    
ubi9/ubi-minimal          rhcc_registry.access.redhat.com_ubi9/ubi-min…   0                    
ubi9/ubi-micro            rhcc_registry.access.redhat.com_ubi9/ubi-mic…   0                    
ubi9-minimal              rhcc_registry.access.redhat.com_ubi9-minimal    0                    
ubi7/php-72               Apache 2.4 with PHP 7.2                         0                    
ubi8/php-72               Platform for building and running PHP 7.2 ap…   0                    
ubi8/s2i-core             Base image which allows using of source-to-i…   0                    
ubi8/s2i-base             Base image with essential libraries and tool…   0                    
ubi8/python-27            Platform for building and running Python 2.7…   0                    
ubi8/python-36            Platform for building and running Python 3.6…   0                    
ubi7/nodejs-8             Platform for building and running Node.js 8 …   0                    
ubi7/ruby-25              Platform for building and running Ruby 2.5 a…   0                    
ubi7/python-27            Python 2.7 platform for building and running…   0                    
ubi8/dotnet-21            .NET Core 2.1 SDK and Runtime on RHEL 8         0                    
ubi7/go-toolset           Platform for building and running Go Applica…   0                    
ubi8/ruby-26              Platform for building and running Ruby 2.6 a…   0                    
ubi8/nodejs-12            Platform for building and running Node.js 12…   0                    
ubi8/php-73               Platform for building and running PHP 7.3 ap…   0                    
ubi7/nodejs-10            Platform for building and running Node.js 10…   0                    
ubi8/dotnet-30            .NET Core 3.0 SDK and Runtime on RHEL 8         0                    
ubi7/php-73               Platform for building and running PHP 7.3 ap…   0                    
ubi7/nodejs-12            Platform for building and running Node.js 12…   0                    
ubi8/openjdk-8            Source To Image (S2I) image for Red Hat Open…   0                    
ubi8/python-38            Platform for building and running Python 3.8…   0                    
ubi8/dotnet-31-runtime    .NET Core 3.1 runtime only on RHEL 8            0                    
ubi8/nginx-118            Platform for running nginx 1.18 or building …   0                    
ubi8/ruby-27              Platform for building and running Ruby 2.7 a…   0                    
ubi8/dotnet-50            .NET 5.0 SDK and Runtime on RHEL 8              0                    
ubi7/nginx-118            Platform for running nginx 1.18 or building …   0                    
ubi8/pause                Podman pod infrastructure container             0                    
ubi8/skopeo               Containerized version of Skopeo                 0                    
ubi8/python-39            Platform for building and running Python 3.9…   0                    
ubi8/httpd-24             Platform for running Apache httpd 2.4 or bui…   0                    
ubi7/ruby-30              Ruby 3.0                                        0                    
ubi8/podman               Containerized version of Podman                 0                    
ubi7/nginx-120            Platform for running nginx 1.20 or building …   0                    
ubi8/openjdk-17           Source To Image (S2I) image for Red Hat Open…   0                    
ubi8/php-80               rhcc_registry.access.redhat.com_ubi8/php-80     0                    
ubi8/perl-532             rhcc_registry.access.redhat.com_ubi8/perl-532   0                    
ubi8/dotnet-21-runtime    Provides the latest release of Red Hat Enter…   0                    
ubi8/perl-526             Platform for building and running Perl 5.26 …   0                    
ubi8/nodejs-10            Platform for building and running Node.js 10…   0                    
ubi8/ruby-25              Platform for building and running Ruby 2.5 a…   0                    
ubi7/python-36            Platform for building and running Python 3.6…   0                    
ubi7/s2i-core             Base image which allows using of source-to-i…   0                    
ubi7/s2i-base             Base image with essential libraries and tool…   0                    
ubi8/dotnet-30-runtime    .NET Core 3.0 runtime only on RHEL 8            0                    
ubi7/ruby-26              Platform for building and running Ruby 2.5 a…   0                    
ubi8/openjdk-11           Source To Image (S2I) image for Red Hat Open…   0                    
ubi8/dotnet-31            .NET Core 3.1 SDK and Runtime on RHEL 8         0                    
ubi7/ruby-27              Platform for building and running Ruby 2.7 a…   0                    
ubi7/python-38            Python 3.8 platform for building and running…   0                    
ubi8/nodejs-14            Platform for building and running Node.js 14…   0                    
ubi8/perl-530             Platform for building and running Perl 5.26 …   0                    
ubi8/php-74               Platform for building and running PHP 7.4 ap…   0                    
ubi8/dotnet-50-runtime    .NET 5.0 runtime only on RHEL 8                 0                    
ubi7/nodejs-14            Platform for building and running Node.js 14…   0                    
ubi8/buildah              Containerized version of Buildah                0                    
ubi8/nodejs-14-minimal    Minimal image for running Node.js 14 applica…   0                    
ubi8/toolbox              Toolbox containerized shell image based in U…   0                    
ubi8/ruby-30              Platform for building and running Ruby 3.0 a…   0                    
ubi8/nginx-120            Platform for running nginx 1.20 or building …   0                    
ubi8/nodejs-16            Platform for building and running Node.js 16…   0                    
ubi8/nodejs-16-minimal    Minimal image for running Node.js 16 applica…   0                    
ubi8/dotnet-60-runtime    .NET 6.0 runtime only on RHEL 8                 0                    
ubi8/dotnet-60            .NET 6.0 SDK and Runtime on RHEL 8              0                    
ubi8/openssl              rhcc_registry.access.redhat.com_ubi8/openssl    0                    
ubi9-beta/toolbox         Toolbox containerized shell image based in U…   0                    
rhel8/toolbox             Toolbox containerized shell image based in U…   0                    
rhel9-beta/toolbox        Toolbox containerized shell image based in U…   0                    
ubi9/toolbox              rhcc_registry.access.redhat.com_ubi9/toolbox    0                    
ubi9/openjdk-11-runtime   rhcc_registry.access.redhat.com_ubi9/openjdk…   0                    
ubi9/openjdk-17-runtime   rhcc_registry.access.redhat.com_ubi9/openjdk…   0                    
e your text here.

 

Create container 

dave@dave ~]$ docker run -it --name  ubi-test registry.access.redhat.com/ubi8/ubi bash
Unable to find image 'registry.access.redhat.com/ubi8/ubi:latest' locally
latest: Pulling from ubi8/ubi
7985264c26f7: Pull complete 
0689084f485b: Pull complete 
Digest: sha256:38e7c463209e3c5b7f1fcb79bb250e4653a66f3be9f23f5d175eeeadf8c3da79
Status: Downloaded newer image for registry.access.redhat.com/ubi8/ubi:latest
[root@634052a6ad62 /]# more /etc/redhat-release 
Red Hat Enterprise Linux release 8.6 (Ootpa)
[root@634052a6ad62 /]# ls /
bin  boot  dev    etc  home  lib    lib64  lost+found  media  mnt  opt  proc  root    run  sbin  srv    sys  tmp  usr  var

Sample custom image


[dave@dave simple]$ more Dockerfile 
FROM registry.access.redhat.com/ubi8/ubi

RUN dnf install -y python3

CMD mkdir /myapp

WORKDIR /myapp

COPY ./myapp.sh .

CMD ./myapp.sh

Build image

$ docker build -t redhat-ubi-myapp .
Sending build context to Docker daemon  4.096kB
Step 1/6 : FROM registry.access.redhat.com/ubi8/ubi
 ---> 16fb1f57c5af
Step 2/6 : RUN dnf install -y python3
 ---> Using cache
 ---> 22a66860e864
Step 3/6 : CMD mkdir /myapp
 ---> Using cache
 ---> 1c724139c1c9
Step 4/6 : WORKDIR /myapp
 ---> Using cache
 ---> 3ca288265be2
Step 5/6 : COPY ./myapp.sh .
 ---> 22db0568addf
Step 6/6 : CMD ./myapp.sh
 ---> Running in 5328a64d06ad
Removing intermediate container 5328a64d06ad
 ---> af70183187f5
Successfully built af70183187f5
Successfully tagged redhat-ubi-myapp:latest

Run image

[dave@dave simple]$ docker run redhat-ubi-myapp
Hello,world!
Red Hat Enterprise Linux release 8.7 (Ootpa)

Saturday, October 15, 2022

Remove deprecated Oracle JDBC classes oracle.sql.ArrayDescriptor, oracle.sql.STRUCT, oracle.sql.StructDescriptor

 HOWTO

 
 
package dave.service;

import java.sql.Array;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Struct;

import javax.annotation.sql.DataSourceDefinition;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import dave.entity.Account;
import oracle.jdbc.OracleConnection;
import oracle.sql.ArrayDescriptor;
import oracle.sql.SQLName;
import oracle.sql.STRUCT;
import oracle.sql.StructDescriptor;

@Stateless
//Data Source defined for JPA. It assume the derby database is started up and listen to localhost:1527
@DataSourceDefinition(name = "java:module/env/mavenArchetypeDataSource", className = "org.apache.derby.jdbc.ClientXADataSource", portNumber = 1527, serverName = "localhost", databaseName = "examples", user = "examples", password = "examples", properties = {
        "create=true", "weblogic.TestTableName=SQL SELECT 1 FROM SYS.SYSTABLES" })
public class AccountManagerImpl implements AccountManager {

    @PersistenceContext
    private EntityManager em;

    public void depositOnAccount(String name, float amount) {
        Account account = em.find(Account.class, name);
        if (account == null) {
            account = new Account();
            account.setName(name);
        }
        account.setAmount(account.getAmount() + amount);
        em.persist(account);
    }

    public Account findAccount(String name) {

        em.getTransaction().begin();
        Connection connection = em.unwrap(java.sql.Connection.class);

        try {
            useDeprecatedOracleConnection(connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        try {
            useOracleConnection(connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return em.find(Account.class, name);
    }

    private void useDeprecatedOracleConnection(java.sql.Connection connection) throws SQLException {

        OracleConnection oracleConnection = null;

        if (connection.isWrapperFor(OracleConnection.class)) {
            oracleConnection = connection.unwrap(OracleConnection.class);
        } else {
            // recover, not an oracle connection
        }

        Object[] reportArray = new Object[3];
        STRUCT[] struct = new STRUCT[1];

        ArrayDescriptor arrayDescriptor = new ArrayDescriptor(
                new SQLName("T_REPORT_TABLE", (OracleConnection) connection), connection);
        StructDescriptor structDescriptor = StructDescriptor.createDescriptor("R_REPORT_OBJECT", connection);

        oracle.sql.ARRAY reportsArray = new oracle.sql.ARRAY(arrayDescriptor, connection, struct);

    }

    private void useOracleConnection(java.sql.Connection connection) throws SQLException {

        OracleConnection oracleConnection = null;

        if (connection.isWrapperFor(OracleConnection.class)) {
            oracleConnection = connection.unwrap(OracleConnection.class);
        } else {
            // recover, not an oracle connection
        }

        Object[] reportArray = new Object[3];
        Struct[] struct = new Struct[1];

        int arrayIndex = 0;

        struct[arrayIndex++] = connection.createStruct("R_REPORT_OBJECT", reportArray);

        Array reportsArray = ((OracleConnection) connection).createOracleArray("T_REPORT_TABLE", struct);

    }
}

Wednesday, May 18, 2022

Disable Spring DEBUG in log4j2 logs on Weblogic

HOWTO 

 

There is dump of all environment variables in Weblogic log due to Spring initialization

2022-05-12 14:13:43,673;DEBUG;com.bea.core.repackaged.springframework.core.env.AbstractEnvironment;<init>();125;Initialized StandardEnvironment with PropertySources

 

To remove it add logger for com.bea.core.repackaged.springframework into log4j.xml file

    <logger name="com.bea.core.repackaged.springframework" additivity="false">
        <level value="error" />
        <appender-ref ref="some-appender"/>
    </logger>

 

Check in Weblogic logs

weblogic.log:<May 18, 2022 10:24:09,804 AM CEST> <Notice> <Stdout> <BEA-000000> <2022-05-18 10:24:09,804 [ACTIVE] ExecuteThread: '0' for queue: 'weblogic.kernel.Default (self-tuning)' DEBUG Level value for com.bea.core.repackaged.springframework is [error].>
weblogic.log:<May 18, 2022 10:24:09,804 AM CEST> <Notice> <Stdout> <BEA-000000> <2022-05-18 10:24:09,804 [ACTIVE] ExecuteThread: '0' for queue: 'weblogic.kernel.Default (self-tuning)' DEBUG com.bea.core.repackaged.springframework level set to ERROR>