How to place folder/jar in Maven META-INF root - java

I am working on a implementation that requires UnlimitedJCEPolicyJDK8 for SAP hana cloud platform. The documentation tells me that i need to place it under the following structure:
WAR file:
META-inf
- ext_security
- jre8
The problem is when i include the jars it goes to WEB-INF/classes and thats not where the server is looking. As seen in the picture.
Technical details:
tomcat 8: v3.2 runtime
JRE 1.8
Maven build: using webapp archatype
With facades to support servlets.
IDE: eclipse
Tried the following:
add files using eclipse web deployment assembly (for what ever the reason it does not seems to work with maven and yes i use .m2 and WTP plugin)
Adding files trough maven dependency and tried copy to output folder.
i open WAR file moved files manually to correct folder and re-zipped and verified that if its in correct folder the update to server is working fine.
i really hope you can help me.
kind regards,
UPDATE: as requested by khmarbaise the pom file. And i created web-inf folder by myself in the hope it would be placed in root.
<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>hcp</groupId>
<artifactId>edi.web</artifactId>
<packaging>war</packaging>
<version>0.0.2-SNAPSHOT</version>
<name>edi.web Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- tried classpath but it did not seem to do annything
Below dependency is from local .m2 repository-->
<dependency>
<groupId>hcp</groupId>
<artifactId>jre8security</artifactId>
<version>1.0.0</version>
<optional>true</optional>
<!-- goes in manifest classpath, but not included in WEB-INF/lib -->
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Solution provided by pace:
create a source folder under the following name: src/main/webapp/META-INF

The maven-war-plugin's war goal has a configuration option webappDirectory. It defaults to src/main/webapp. Anything in there will be placed in the root of the war. So you could create a directory:
src/main/webapp/META-INF
and drop in whatever files you want there. I don't much about the web tools plugin so not sure what approach would work there.

Related

Is it possible to have Maven "WAR" pom.xml package up my classes up in a JAR and put the JAR in the /WEB-INF/lib folder?

I have a simple Java Spring MVC web app. I build the WAR file using a Maven pom.xml file.
When I do a "maven install", it produces a WAR file with my compiled Java classes in the /WEB-INFO/classes folder.
My maven pom.xml looks like this (dependencies commented out) ...
<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>mycompany.com</groupId>
<artifactId>myapp</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>My Application</name>
<url>http://maven.apache.org</url>
<dependencies>
<!-- .... -->
</dependencies>
<build>
<finalName>myapp</finalName>
</build>
</project>
Next I want to obfuscate my Java classes (I'll be using Allatori obfuscater), so I'm thinking the easiest thing would be if my Java classes were all put into their own JAR file and stored in the /WEB-INF/lib folder with the rest of the JARs.
Is there a way to modify my Maven pom.xml file so it will package of my classes up in a JAR and put the JAR in the /WEB-INF/lib folder?
UPDATE:
Adding this to the 'build' section (as suggested by "JF Meier" worked) ...
<build>
<finalName>myapp</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archiveClasses>true</archiveClasses>
</configuration>
</plugin>
</plugins>
</build>
You can use the <archiveClasses> configuration (set it to true) to generate an additional jar with the classes.
Also see:
https://maven.apache.org/plugins/maven-war-plugin/war-mojo.html
Move your Java classes in a separate Maven module and add that as a dependency to your WAR project. Then you can do whatever is necessary creating the jar in that module.

spring-boot Maven: How to create executable jar with main class?

Recently, I wrote a Spring-Boot project and I wanted that Maven will be able to create a jar file that I'll be able to run by the command "java -jar ".
This is the pom.xml I wrote:
<?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>SpringBootGame</groupId>
<artifactId>SpringBootGame</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.0.3.RELEASE</version>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.0.3.RELEASE</version>
<configuration>
<mainClass>com.game.Main</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
In order to build the jar file, I had to run the command: "mvn clean package spring-boot:repackage".
My questions are:
Why must I add spring-boot-maven-plugin? I already have spring-boot-starter-web dependency that adds spring-boot as a dependency and maven-compiler-plugin that builds a jar file from the code?
Can you think about a way to configure pom.xml file that I'll be able to get the jar file using the "native" command "mvn clean package" and not the cumbersome "mvn clean package spring-boot:repackage"?
Thanks
Why must I add spring-boot-maven-plugin? I already have spring-boot-starter-web dependency that adds spring-boot as a dependency and maven-compiler-plugin that builds a jar file from the code?
Because the plugin is what adds Maven support for Spring Boot
Can you think about a way to configure pom.xml file that I'll be able to get the jar file using the "native" command "mvn clean package" and not the cumbersome "mvn clean package spring-boot:repackage"?
It looks like you are missing the <packaging>jar</packaging> element in between your <project> </project> element.
The reason you had to run that lengthy command, is because you did not include the <executions></executions> element when including the plugin. Please see section 71.1 of the following docs:
https://docs.spring.io/spring-boot/docs/current/reference/html/build-tool-plugins-maven-plugin.html#build-tool-plugins-include-maven-plugin
71.2 elaborates on the <packaging> element.
In the configuration of the maven plugin you have already in place, you need to add the executable block like :
<configuration>
<executable>true</executable>
</configuration>
this will create in the target folder the runnable jar that can be run by using java -jar command.

maven war plugin does not forget about old files and dependencies and creates a war that contains removed files and dependencies

Hello I'm having a weird problem about war creation.
I use maven-war-plugin (tried with 3.1.0, 3.2.0, and 2.6). When I run war:war (or mvn clean package, or similars) the war is created but I noticed that it is like it always packages all the files and dependencies it encountered in the history of the project. I noticed this because the war file is always getting bigger and by opening it I see there are a lot of dependencies that I declared on the pom but now they are removed, and most importantly there are classes I deleted from the project! I even tried to start a new project and the result does not change.
I guess I'm using this badly... Is there something I should do to let him "forget" about the history and force it to consider the project "as it is" when creating a war?
Thanks for the help!
PS:
I always clean and compile before running war:war;
I work with IntelliJ IDEA Community 2017.3
using jdk 1.7 because of technical constraints.
Just for completeness, there is my 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>sduca-consumer-listener</groupId>
<artifactId>sduca-consumer-listener</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<webappDirectory>/sduca</webappDirectory>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- Using Spring 4 because of jdk 1.7 -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
</dependencies>
</project>
EDIT1: I tried to change versions of maven-war-plugin, maven-compiler-plugin, and Maven itself, but nothing changes.
EDIT2: I tried changing the output directory, no changes. I tried to change version of the dependencies and as "expected" now the war includes both versions, becoming bigger and bigger...
EDIT3: I reinstalled IntelliJ IDEA, upgrading to the latest version. Nothing changes, it has only reset the content of the package, but it still "keep track" of the content and dependencies and still includes removed stuff on the package when creating a new one.
EDIT4: I've started using mvn clean compile war:war instead of mvn clean package and at first things started working, but then the problem of the existing old files returned. However, I find out that if I manually remove the exceding resources and *.class from the war it looks it works (at least it's deployed).
It's just a trick worked for me many times,
Backup your dependencies from pom.xml and delete it.
Do a clean build, it will show errors in all Java classes. Also delete the previous war.
Now, replace those backed up dependencies and clean build the webapp again. All errors will be disappeared and war file will be as expected.

Manually launched Spring Boot application from .jar not working properly

The problem is when I run .jar file(Spring Boot MVC app) from Intellij IDEA, application works fine, but when I try to run same file from the command line, template resolver cannot find template(.html) files. And this seems to be reasonable since when I opened .jar with file archiver I did not find any traces of that files.
How Intellij make it work proper? And how can I do the same?
My project structure:
here they are, in '/templates' folder
.jar file opened in archiver:
As I said, there is no .html files, so how Intellij add them when running .jar?
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>com.vlad.pet.contactlist</groupId>
<artifactId>web-app</artifactId>
<version>1.0-ALPHA</version>
<packaging>jar</packaging>
<name>web-app</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.6.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>com.vlad.pet.contactlist</groupId>
<artifactId>model</artifactId>
<version>1.1-inmemory</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
A normal Spring Boot application built from http://start.spring.io would show the templates folder within the src/main/resources. This is because by default the contents of that folder are made available to the classpath and packaged at the root of the jar file. I am guessing you modified the project in IntelliJ to also pick up the src/main/webapp directory. Unfortunately that doesn't work when it comes to packaging the jar. You would need to work with the maven resources plugin to specify the additional directory to be included in the jar file.
My recommendation is to go to start.spring.io. Select Web and Thymeleaf as dependencies and then download the zip file. When you look in that file you will see the default structure along with settings for any plugins to make it work. Mirror that structure within you application.
If you don't have the ability to change the project structure then check out the maven-resources-plugin for specifics on how to add those directories.
You can see what exactly IntelliJ is doing, by looking at first line in its console after running app. There should be full command called by IntelliJ.
And how .jar is build? Maven/Gradle? maybe there's some problem with pom/build script
It should be comment, but my reputation is too low
Try to use the plugin to generate an executable jar:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
After building the jar (e.g. mvn clean install), you can run the jar as java -jar ./target/project.jar

Problems converting Dynamic Web Project to Maven

I'm using Eclipse Luna with Websphere Application Sever 8.5.
When I run a dynamic web project using the Websphere Application Sever 8.5 it runs fine.
However, when I convert a dynamic web project to a maven project, i'm getting around 53 errors starting with Missing artifact com.ibm.websphere e.g Missing artifact com.ibm.websphere.xml:xmlapi:jar:1.0.0' all located in my pom.xml.
I am also getting this error:
The container 'Maven Dependencies' references non existing library
C:\Users\Brian242\.m2\repository\com\ibm\websphere\ws\com.ibm.ws.wccm\1.0.0\com.ibm.ws.wccm-1.0.0.jar'
Not sure if that's part of the problem.
I've looked everywhere for a solution but haven't found anything. So I have decided to ask for some help. Does anyone know how to solve the above problem/s?
Thank you,
Brian
<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>TrialProj</groupId>
<artifactId>TrialProj</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>com.ibm.tools.target</groupId>
<artifactId>was</artifactId>
<version>8.5.5</version>
<type>pom</type>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
you created M2_REPO environment variable?
right click on the project root
Build Path-> Configure Puild Path ...
Click button 'Add Variable'
Click 'Set Variable ...'
Click 'New ...'
name: M2_REPO
Path: / path_to_repository_the_maven /
Click Ok
Recompile
If you are using eclipse make sure you add suport for eclipse web tools using:
mvn eclipse:eclipse -Dwtpversion=2.0
(2.0 or your required version) And afterwards recompile maven project:
mvn compile

Categories