display application version in title using thymeleaf and springboot - maven

I want to display in my htm page the version of my webapp, using something like this (thymeleaf inside) :
<h4 th:text="${version}">Version</h4>
The data is well set in the pom.xml :
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>fr.test.navig</groupId>
<artifactId>navigo</artifactId>
<version>2.0.3-SNAPSHOT</version>
...
<!-- Package as an executable jar -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>Application</mainClass>
<addDefaultImplementationEntries>
true
</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
and I can see it in the MANIFEST.MF (which is in the generated jar under META-INF) :
Implementation-Version: 2.0.3-SNAPSHOT
I've tried to get the appplication version in the controller and set it in a ModuleAttribute :
#ModelAttribute("version")
public String getVersion() {
logger.info("ModelAttribute to get application version");
return getClass().getPackage().getImplementationVersion();
}
But getClass().getPackage().getImplementationVersion() value is null. Indeed the package implementationVersion is not the implementation Version of the application by default.

I know I'm late but Patrick's answer and Spring docs greatly helps in this matter.
1. If your pom.xml use spring-boot-starter-parent as parent, you can use #project.version# to get version (and any other Maven properties) in your application.properties file. According to Spring docs:
You can automatically expand properties from the Maven project using
resource filtering. If you use the spring-boot-starter-parent you
can
then refer to your Maven ‘project properties’ via #..# placeholders
Maven pom.xml:
<groupId>com.foo</groupId>
<artifactId>bar</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Foo</name>
<description>Bar</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
Spring application.properties:
foo.app.version=#project.version#
2. Then a class annotated with #ControllerAdvice can be used to inject version as model attribute.
#ControllerAdvice
public class ControllerAdvice {
#Value("${foo.app.version}")
private String applicationVersion;
#ModelAttribute("applicationVersion")
public String getApplicationVersion() {
return applicationVersion;
}
}
3. Finally this model attribute can be accessed by Thymeleaf as any other.
<th:block th:text="${applicationVersion}"></th:block>
Hope this helps!

Here is the simplest way I've found :
In my controller :
#ModelAttribute("version")
public String getVersion() throws IOException {
logger.info("ModelAttribute to get application version");
Manifest manif = new Manifest(
Application.class.getResourceAsStream("/META-INF/MANIFEST.MF"));
String version = (String) manif.getMainAttributes().get(
Attributes.Name.IMPLEMENTATION_VERSION);
return version;
}
In my htm page :
<h4 th:text="${version}">Version</h4>

You need to configure resource plugin to activate filtering on the file that need to be enriched with properties coming from your POM file.
In the generated war, the version (in fact ${project.version}) will be hardcoded to your POM version.

Related

Is there any alternatives to registerColumnType on upgrade from Hibernate-core:5.6.10.Final to 6.1.5.Final

I am currently upgrading from Spring-boot-starter-parent 2.7.3 to 3.0.0. The hibernate-core dependency for this is 6.1.5.Final.
This has broken my custom SQL dialect I had implemented.
The server dialect class is below:
public class ServerDialect extends SQLServer2012Dialect {
public ServerDialect() {
super();
registerColumnType(Types.NVARCHAR, "nvarchar(max)");
registerHibernateType(Types.NVARCHAR, StandardBasicTypes.STRING.getName());
}
}
Since upgrade, these method's can no longer be resolved.
All I have changed is:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.3</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
to
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.0</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
I have looked up the documentation and searched for alternatives with no success.
It might be worth evaluating if this Dialect is required for your project post upgrade - prior to Hibernate 6 it was common to provide the Dialect version via the hibernate.dialect setting, but it is no longer the recommended strategy. More information on this can be found in the documentation.
If this is not a suitable resolution, there is however a workaround for your issue.
SQLServer2012Dialect is now deprecated so as part of this migration should should consider refactoring your custom dialect to use the recommended SQLServerDialect.
You should be able to make use of resolveSqlTypeDescriptor to achieve the same or similar functionality - for example;
#Override
public JdbcType resolveSqlTypeDescriptor(String columnTypeName,
int jdbcTypeCode,
int precision,
int scale,
JdbcTypeRegistry jdbcTypeRegistry) {
switch (jdbcTypeCode) {
case Types. NVARCHAR, Types.CHAR -> jdbcTypeCode = StandardBasicTypes.STRING.getSqlTypeCode();
// ETC
}
return super.resolveSqlTypeDescriptor(
columnTypeName,
jdbcTypeCode,
precision,
scale,
jdbcTypeRegistry
);
}
However, given your example above - this should not be required as the newer Hibernate should automatically perform an NVARCHAR mapping like this for you.

Building SOA composite application using maven

Using SOA suite 11, trying building source code (composite.xml with configuration file) in SOA composite application into a jar file using maven.
Can anyone help to guide me making POM.xml for the same. i am using "Apache-ANT-Plugin" in my pom file.
http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
This POM relates to this SOA Composite, i.e. the one in this same directory.
There is another POM in the SOA Application directory which handles
the whole SOA Application, which may contain additional projects.
-->
4.0.0
test1
HelloWrold
1.0-SNAPSHOT
sar
<!--
The parent points to the common SOA parent POM. That is a special POM that is
shipped by Oracle as a point of customization (only). You can add default values
for properties like serverUrl, etc. to the SOA common parent POM, so that you
do not have to specify them over and over in every project POM.
-->
<parent>
<groupId>com.oracle.soa</groupId>
<artifactId>sar-common</artifactId>
<version>12.1.3-0-0</version>
</parent>
<properties>
<!-- These parameters are used by the compile goal -->
<scac.input.dir>${project.basedir}\SOA/</scac.input.dir>
<scac.output.dir>${project.basedir}/target</scac.output.dir>
<scac.input>${scac.input.dir}/composite.xml</scac.input>
<scac.output>${scac.output.dir}/out.xml</scac.output>
<scac.error>${scac.output.dir}/error.txt</scac.error>
<scac.displayLevel>1</scac.displayLevel>
<!-- if you are using a config plan, uncomment the following line and update to point
to your config plan -->
<!--<configplan>${scac.input.dir}/configplan.xml</configplan>-->
<!-- These parameters are used by the deploy and undeploy goals -->
<composite.name>${project.artifactId}</composite.name>
<composite.revision>1.0</composite.revision>
<composite.partition>default</composite.partition>
<serverUrl>${oracleServerUrl}</serverUrl>
<user>${oracleUsername}</user>
<password>${oraclePassword}</password>
<overwrite>true</overwrite>
<forceDefault>true</forceDefault>
<regenerateRulebase>false</regenerateRulebase>
<keepInstancesOnRedeploy>false</keepInstancesOnRedeploy>
<!-- These parameters are used by the test goal
if you are using the sca-test (test) goal, you need to uncomment the following
line and point it to your jndi.properties file. -->
<!--<jndi.properties.input>UNDEFINED</jndi.properties.input>-->
<scatest.result>${scac.output.dir}/testResult</scatest.result>
<!-- input is the name of the composite to run test suties against -->
<input>${project.artifactId}</input>
<!--<scac.ant.buildfile>${env.MW_HOME}/soa/bin/ant-sca-compile.xml</scac.ant.buildfile>
<sca.ant.testfile>${env.MW_HOME}/soa/bin/ant-sca-test.xml</sca.ant.testfile>
-->
</properties>
<build>
<plugins>
<plugin>
<groupId>com.oracle.soa.plugin</groupId>
<artifactId>oracle-soa-plugin</artifactId>
<version>12.1.3-0-0</version>
<configuration>
<compositeName>${project.artifactId}</compositeName>
<composite>${scac.input}</composite>
<sarLocation>${scac.output.dir}/sca_${project.artifactId}_rev${composite.revision}.jar</sarLocation>
<serverUrl>${serverUrl}</serverUrl>
<user>${user}</user>
<password>${password}</password>
<!-- Note: compositeRevision is needed to package, revision is needed to undeploy -->
<compositeRevision>${composite.revision}</compositeRevision>
<revision>${composite.revision}</revision>
<scacInputDir>${scac.input.dir}</scacInputDir>
<input>${input}</input>
</configuration>
<!-- extensions=true is needed to use the custom sar packaging type -->
<extensions>true</extensions>
</plugin>
</plugins>
</build>

find path to JAX-RS resource

I just can't find out the path to access my JAX-RS resource which is deployed to wildfly.
ear.ear pom:
<parent>
<artifactId>jee-services</artifactId>
<groupId>org.mycompany</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<packaging>ear</packaging>
<modelVersion>4.0.0</modelVersion>
<artifactId>ear</artifactId>
<dependencies>
<dependency>
<groupId>org.mycompany</groupId>
<artifactId>ejb_book</artifactId>
<version>1.0-SNAPSHOT</version>
<type>ejb</type>
</dependency>
</dependencies>
ejb_book pom:
<parent>
<artifactId>jee-services</artifactId>
<groupId>org.mycompany</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>ejb</packaging>
<artifactId>ejb_book</artifactId>
application config
#ApplicationPath("/resources")
public class ApplicationConfig extends Application {
}
resource
#Stateless
#Path("/books")
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
public class BookResource extends AbstractFacade<Book> {
#PersistenceContext
private EntityManager entityManager;
#GET
#Path("/getBooks")
public Book getBook() {
return new Book();
}
I think the issue is that I'm packaging my ejb_book.jar into ear.ear (where I collect all other ejb-modules)
I tried:
localhost:8080/ear/resources/books/getBooks
and many other combinations but none of them worked.
The application deploys fine to the WildFly server.
BTW: is there a tool to help people access their JAX-RS resource? From the IDE for example. So my question wouldn't be a problem anymore.
According to the JAX-RS 2.0 specification, section 2.3.2 Servlet
A JAX-RS application is packaged as a Web application in a .war file.
Since you're not packaging your application as a WAR module and neither you have a WAR module in your EAR, you're not providing a context-root to your (web) application.
This is preventing you from accessing your REST API through the desired way:
http://localhost:8080/<context-root>/resources/books/getBooks
As a solution, you could either package your entire application as a WAR, placing the application classes in WEB-INF/classes or WEB-INF/lib and required libraries in WEB-INF/lib, or you could maintain the use your EAR, adding to it a WAR module.

scala Ide Not identifying my class as main method

I have researched some answers in Stackoverflow but my error seems to be different. Having said that i am a newbie in scala hence please consider that as well.
I am trying to create multi class project in scala. Project Format is like this
Test Project
|
COM Package
|
|-->App.class
|
COM.Test Package
|
|-->App1.class
Code Snippet
App.scala
object App {
def main(args : Array[String]): Unit = {
var logger = Logger.getLogger(this.getClass())
if (args.length < 3) {
logger.error("=> wrong parameters number")
System.err.println("Usage: MainExample <path-to-files> <srcCode> <tableName>")
System.exit(1)
}
println("In Main Class")
}
App1.scala
object App1 {
def main(args : Array[String]): Unit = {
println("In Sub Class")
}
Pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com</groupId>
<artifactId>test</artifactId>
<version>1.0.0</version>
<name>${project.artifactId}</name>
<description>My wonderfull scala app</description>
<inceptionYear>2015</inceptionYear>
<licenses>
<license>
<name>My License</name>
<url>http://....</url>
<distribution>repo</distribution>
</license>
</licenses>
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<encoding>UTF-8</encoding>
<scala.version>2.10.4</scala.version>
<scala.compat.version>2.10.4</scala.compat.version>
</properties>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala.version}</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/scala</sourceDirectory>
<!-- >testSourceDirectory>src/test/scala</testSourceDirectory -->
<!-- plugins>
<plugin>
<see http://davidb.github.com/scala-maven-plugin >
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<args>
<arg>-make:transitive</arg>
<arg>-dependencyfile</arg>
<arg>${project.build.directory}/.scala_dependencies</arg>
</args>
</configuration>
</execution>
</executions>
</plugin>
</plugins -->
</build>
</project>
I am using SCALA IDE along with Scala Maven Plugin. I have commented out the build plugin but it doen=s not make a difference if that is turned on as well.
Two Issues i am facing
Issue 1) I am unable to Run App1.scala in Sub Package COM.Test. App.scala is run without issue from Run As -->Scala Application. But when ever i try to run App1.scala the scala side is not able to find Main Class
Issue 2) I am unable to debug my code for App.scala in scala ide. Whenever i run Debug Command (Bug Operator) the breakpoints are skipped and code executes entirely. I use Scala JVM Launcher and checked The option Stop in Main. Please help
Using Eclipse Luna with Scala IDE and MAC OS
Package structure must correspond to directory structure in the IDE. (This is unlike scalac.)
It's hard to tell from the info you present, but I can confirm that Scala IDE (on Eclipse Luna) will not offer "Run As > Scala application" if I remove the package declaration.
For example, if the class App in directory src/p/ (source file src/p/App.scala) is not in package p, then it won't offer to run it.
Uncommenting the correct package declaration gets the run as menu back.
I can also verify that a debug config with stop in main and Scala JVM launcher works as expected for App in the default package as well as in a named package. So I don't know how that could have broken for you.
You need to add extends App to your main objects.
object App extends App {
def main(args : Array[String]): Unit = {
var logger = Logger.getLogger(this.getClass())
if (args.length < 3) {
logger.error("=> wrong parameters number")
System.err.println("Usage: MainExample <path-to-files> <srcCode> <tableName>")
System.exit(1)
}
println("In Main Class")
}

NullPointerExpection on basic EJB application in NetBeans 7.3.1

I'm doing a course on Enterprise computing using Beginning Java EE 6 Platform with GlassFish 3: From Novice to Professional
Last chapter was about EJB and I've actually found it very hard to understand.
I've been trying to run one of the book's sample codes in order to try to better understand EJB but I'm getting a NullPointerException whenever I call for the EJB.
I'm using NetBeans 7.3.1 and the Maven and Glassfish versions provided with NetBeans (Maven seems to be 3.0.5, and Glassfish is 4.0)
This is the code for the main class:
package org.beginningee6.book.chapter06;
import javax.ejb.EJB;
/**
* #author Antonio Goncalves
* APress Book - Beginning Java EE 6 with Glassfish
* --
*/
public class Main {
// ======================================
// = Attributes =
// ======================================
#EJB
private static BookEJBRemote bookEJB;
// ======================================
// = Public Methods =
// ======================================
public static void main(String[] args) {
// Creates an instance of book
Book book = new Book();
book.setTitle("The Hitchhiker's Guide to the Galaxy");
book.setPrice(12.5F);
book.setDescription("Science fiction comedy series created by Douglas Adams.");
book.setIsbn("1-84023-742-2");
book.setNbOfPage(354);
book.setIllustrations(false);
book = bookEJB.createBook(book);
System.out.println("### Book created : " + book);
book.setTitle("H2G2");
book = bookEJB.updateBook(book);
System.out.println("### Book updated : " + book);
System.out.println("Execution succeeded");
}
}
Here is the persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="chapter06PU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<!--<jta-data-source>jdbc/__default</jta-data-source>-->
<jta-data-source>jdbc/chapter06DS</jta-data-source>
<class>org.beginningee6.book.chapter06.Book</class>
<properties>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
<property name="eclipselink.logging.level" value="INFO"/>
</properties>
</persistence-unit>
</persistence>
And this is the pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.beginningee6.book</groupId>
<artifactId>chapter06</artifactId>
<packaging>jar</packaging>
<version>2.0</version>
<name>Week5</name>
<parent>
<groupId>org.beginningee6.book</groupId>
<artifactId>chapters</artifactId>
<version>2.0</version>
</parent>
<dependencies>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>${javax.persistence-version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>${eclipselink-version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>${glassfish-version}</version>
</dependency>
</dependencies>
<!--To avoid multiple modules with Maven, here is what you need to manually do (it's not nice, but it works)
1) Comment the following section (maven-jar-plugin), package the jar, and deploy to GlassFish
2) Uncomment the following section, package the jar and run the Main class with app client -->
<build>
<!-- <plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>${plugin-jar-version}</version>
<configuration>
<archive>
<manifest>
<mainClass>org.beginningee6.book.chapter06.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>-->
</build>
</project>
I'm getting this error:
Exception in thread "main" java.lang.NullPointerException
at this line:
book = bookEJB.updateBook(book);
Apparently Maven is giving this error:
Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:exec (default-cli) on project chapter06: Command execution failed. Process exited with an error: 1 (Exit value: 1) -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:exec (default-cli) on project chapter06: Command execution failed.
And after activating Maven's debugging option the last command line before the error is:
Executing command line: C:\Program Files (x86)\Java\jdk1.7.0_21\bin\java.exe -classpath C:\Users\Manuel_Laptop\Desktop\Week5\COIT20227LabSolWeek5\Week5\target\classes;C:\Users\Manuel_Laptop\.m2\repository\org\eclipse\persistence\javax.persistence\2.0.0\javax.persistence-2.0.0.jar;C:\Users\Manuel_Laptop\.m2\repository\org\eclipse\persistence\eclipselink\2.0.1\eclipselink-2.0.1.jar;C:\Users\Manuel_Laptop\.m2\repository\org\glassfish\extras\glassfish-embedded-all\3.0.1-b19\glassfish-embedded-all-3.0.1-b19.jar org.beginningee6.book.chapter06.Main
After going through some course tips and some forums I went and set my JAVA_HOME and M2_HOME to their respective directories but nothing worked. I even found a link to someone having an issue on the same chapter of the textbook (https://getsatisfaction.com/javaee6/topics/yet_another_chapter_6_ejb_problem) after seeing it referenced here in StackOverflow, but couldn't find anything there (didn't understand it to be honest)
It's not maven issue, you should set up ejb container in java SE enviroment to use EJB.
Try someithing like this
EJBContainer.createEJBContainer()
you can get complete example at http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/embeddedContainerDemo/EmbeddedContainerDemo.html

Resources