Maven Build - Class Files Size is reduced - java

I am trying to mavenize an existing project.
I was able to build the EAR file(since i have to deploy in Websphere), When I try to deploy, using admin console - Able to install successfully , But application is not working, After investigating, I found the class files size is very less compare to the reference EAR file(old existing EAR file)
Steps I followed to build the EAR file
M2E plugin installed
Configure to Maven
Add ALL the jar files from lib folder like below(I read in SO, this is not the recommended way, but to complete the project, I have to do this)
<dependency>
<groupId>JarFile</groupId>
<artifactId>JarFile</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${basedir}/WebContent/WEB-INF/lib/CRDBXMLExternal.jar</systemPath>
</dependency>
Added the relevant plugins (war, EAR)
Clean Build and Install.
ear file created. ear contains a war file, which has all the project related files including class,jsp etc.
I compared the folder structure with the existing EAR file and its contents , all look good. But only the size of class files(Not ALL but more than 80%) are varying. I use JD to decompile and see the code, Most of the code are not present, including imports.
If anyone has encountered similar issue , could you please tell me what am doing wrong here.
More Info
there are two project folders(both are maven) one will create WAR and another one EAR in EAR pom.xml
there is a dependency
<dependency>
<groupId>com.comp.abc</groupId>
<artifactId>abc</artifactId>
<version>1.0</version>
<type>war</type>
</dependency>
Then there is a plugin
<build>
<plugins>
<plugin>
<artifactId>maven-ear-plugin</artifactId>
<version>2.10</version>
<configuration>
<version>5</version>
<defaultLibBundleDir>lib</defaultLibBundleDir>
<generatedDescriptorLocation>C:\COMP\Dev\may\repos\0.0.1-SNAPSHOT</generatedDescriptorLocation>
</configuration>
</plugin>
</plugins>
</build>
Adding WAR file building(Removed most of the dependencies kept only one sample) 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.comp.abc</groupId>
<artifactId>abc</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<name>ABC</name>
<description>ABC</description>
<dependencies>
<!-- Local Repository -->
<dependency>
<groupId>com.ibm.ws.runtime</groupId>
<artifactId>com.ibm.ws.runtime</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${basedir}/WebContent/WEB-INF/lib/com.ibm.ws.runtime.jar</systemPath>
</dependency>
</dependencies>
<repositories>
<repository>
<id>nexus-releases</id>
<name>nexus</name>
<url>http://abc-nexus.ldn.xyz.com:9080/nexus/content/groups/public/</url>
</repository>
</repositories>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<!-- <warSourceDirectory>${project.basedir}\WebContent</warSourceDirectory> -->
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
</project>

The above does not tell Maven to package the EAR file with the lib directory dependency. It actually tells it to create local dependency on an existing JAR that is provided only at compile time. Thus, when you export the EAR, it does not include any of the JARs because it assumes that they are provided at runtime.
You should use the maven-ear-plugin which package an EAR file instead. You can find the full documentation here.
<plugin>
<artifactId>maven-ear-plugin</artifactId>
<version>3.0.1</version>
</plugin>

The issue was,
web/WebContent/WEB-INF/classes is not getting updated.
but I could see the latest classes under web/target/classes path.
Now am checking why web/WebContent/WEB-INF/classes is not getting updated.
Just now got the Resolution from the below Link :-
ISSUE SOLVED by with the help of
https://coderanch.com/t/474423/ide/ecplise-doesn-create-classes-folder
Steps
Right click on your project -> build path -> Configure build path -> click on source tab -> click on browse (Default output folder).
After browsing click on WebContent -> Select WEB-INF -> Create new folder (called classes). it will open new window.
Give folder name as classes. Click on Advanced and give path of current classes folder means WEB-INF/classes.
After doing this, eclispe will rebuild your project and classes will be genenrated at WEB-INF/classes directory.

Related

Maven - Executable file from a java project

I need to use maven (for a school project) to create an executable file from a single maven command. I've never used maven and tried many solutions here on stackoverlow. The solutions created a jar file, but the file never opened.
This is my project structure
src
com
project
code
swing
programm
interface
Main.class
I know this isn't maven convention, however changing it now would mean I would have to adjust the imports (as intelliJ doesn't refactor everything perfectly) for around 40 classes.
<?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>MyGroup</groupId>
<artifactId>myProgramm</artifactId>
<version>0.7-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Hello World</name>
<description>Course Project</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc -->
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.25.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit/junit5-engine -->
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit5-engine</artifactId>
<version>5.0.0-ALPHA</version>
</dependency>
</dependencies>
<build>
</build>
What do I have to put inside to make an executable file?
TimurJD's answer is correct however I would like to explain step by step what is actually happening and why.
To have a jar be executable the JVM needs to know where your main method is.
For that you need a file called META-INF/MANIFEST.MF inside the jar you create.
This file must contain a reference to the class containing your main method which is done like this:
Main-Class: com.example.ClassContainingMainMethod
There are many ways of creating said file but since you are using maven here is the plugin that will help you create this manifest file
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.my.packege.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
Once you have the plugin in your pom.xml simply run the maven install goal, either from your IDE or the command line. After you should find a folder called target in your project. That folder will contain the executable jar.
To run the jar you can call from the command line:
java -jar MyProject.jar
It should also be noted that unless you abide by the maven standard of keeping your source code in src/main/java you will have to specify your source folder explicitly.
You need to add plugin to your pom.xml file
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>com.example.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
and to run the program: mvn clean install exec:java
... here is the link for doc http://www.mojohaus.org/exec-maven-plugin/usage.html
There are possible different solutions, depends on your requirements: https://www.baeldung.com/executable-jar-with-maven

Best way to build twice the same webapp with different dependencies using maven

I have a maven project with a webapp for which I need two versions, each one having its own set of dependencies. The intent is to support two different (and conflicting) versions of a storage client. The webapp code, configuration file and anything but certain libraries is the same in both cases. The right client is loaded at runtime : I just need to drop the right jar (and its dependencies) in the lib folder of the webapp.
If I deploy the dependencies manually, I lose the opportunity to check for version conflicts (which I do when I build a maven project with all its dependencies correctly set).
I do not want to deploy the webapp(s) on the maven repository since it is not a library and it only makes a big archive (mainly because of the embedded dependencies) that consumes space for nothing. Thus, to build the final wars, I cannot add a dependency on the webapp project.
I do not want to duplicate the common webapp class files and configuration files in two different modules. It would make future evolutions more difficult because of the necessary synchronization between the two modules each time one file is updated.
Any suggestion on how to solve this ?
Note that the best solution should allow to build both wars at once.
Use Maven profiles.
http://maven.apache.org/guides/introduction/introduction-to-profiles.html
You can put certain dependencies into certain profiles and activate/deactivate them through the command line with the -P parameter.
I guess defining two profiles in your pom might do the trick :
<project [...]>
[...]
<profiles>
<profile>
<id>storage1</id>
<dependencies>
<dependency>
<groupId>my.group.storage</groupId>
<artifactId>thisOne</artifactId>
<version>13</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>storage2</id>
<dependencies>
<dependency>
<groupId>my.group.storage</groupId>
<artifactId>thisOtherOne</artifactId>
<version>37</version>
</dependency>
</dependencies>
</profile>
</profiles>
[...]
</project>
Call one or the other with mvn -P storage1 or mvn -P storage2. You can also make one active by default, use activation triggers based on other properties, etc.
Here's their introduction article.
In the end, I did not use profiles. There was an issue building both webapp versions at once.
Instead I used war overlays https://maven.apache.org/plugins/maven-war-plugin/overlays.html.
First, I created a skinny war version of the webapp. The skinny war does not include libraries nor META-INF files. Only resources like configuration files. The webapp classes are packaged in a jar (using the attachedClasses configuration option of the maven-war-plugin). I do not mind having this war deployed since it is very lightweigth. Here is the configuration of the maven-war-plugin :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<outputFileNameMapping>#{groupId}#.#{artifactId}#-#{version}##{dashClassifier?}#.#{extension}#</outputFileNameMapping>
<attachClasses>true</attachClasses>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
<packagingExcludes>WEB-INF/classes/**/*,WEB-INF/lib/*</packagingExcludes>
</configuration>
</plugin>
Then, I created 2 additional modules, one for each flavour of the webapp. In the dependencies, I set :
- the webapp as a dependency of type war
- the jar of the webapp classes
- the storage client library
That way, maven checks for dependency conflicts in all the libraries. The webapp classes are imported through the dependency. The overlay war is used to build the final war. No duplicate code between the 2 flavours of the webapp. Only the client dependency changes between the 2 pom files. Here is an excerpt of one of them :
<dependencies>
<dependency>
<groupId>com.storage</groupId>
<artifactId>client</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.group.id</groupId>
<artifactId>webapp</artifactId>
<version>${project.version}</version>
<classifier>classes</classifier>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.group.id</groupId>
<artifactId>webapp</artifactId>
<version>${project.version}</version>
<type>war</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>

Is there any difference between war file created by maven package phase and war file created by maven war plugin war:war goal

I would to know if a war file created using maven package phase would be equal to a war file created using maven war plugin war:war goal.
Assuming we have a pom.xml (extract) like this:
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<webappDirectory>/sample/servlet/container/deploy/directory</webappDirectory>
</configuration>
</plugin>
</plugins>
</build>
...
I mean, it's necessary to have the plugin to build a war file (with no special restriction or feature). Please fee free to add any comment or suggestion. Thanks in advance
They are absolutelly equal. The purpose of plugin block with maven-war-plugin description is - for example - change default webappDirectory value. See more: http://maven.apache.org/plugins/maven-war-plugin/usage.html

downloading spring petclinic by installing tomcat 7 maven plugin from command line

I am trying to download a fresh copy of the java spring petclinic sample application, but am having trouble getting it from the windows 7 command line because the tomcat7 plugin is not installed. I therefore took the pom.xml syntax from this page, put the resulting pom.xml in the same directory as the command line was focused on, and typed the following into the command line:
mvn clean install tomcat7:run
But I got a long error message. Can someone show me how to fix the pom.xml (and anything else) so that I can download the maven tomcat7 plugin to a stable location where maven will always know where it is?
Here is the pom.xml I am using:
<?xml version="1.0" encoding="UTF-8"?>
<project>
<build>
<!-- To define the plugin version in your parent POM -->
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
</plugin>
</plugins>
</pluginManagement>
<!-- To use the plugin goals in your POM or parent POM -->
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
</plugin>
</plugins>
</build>
</project>
Here is a screen shot of the error message:
EDIT:
Here is my updated pom.xml, which ran successfully, but left me without the ability to follow the next step in these instructions:
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<build>
<!-- To define the plugin version in your parent POM -->
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
</plugin>
</plugins>
</pluginManagement>
<!-- To use the plugin goals in your POM or parent POM -->
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
</plugin>
</plugins>
</build>
</project>
So now how do I get the spring petclinic sample application to download?
Following the instructions at this link, it is not available at either or the following addresses:
http://localhost:9966/petclinic/
http://localhost:8080/petclinic/
The error message is exactly telling you what is wrong.
'modelVersion' is missing
Take a look at some example: Introduction to the POM. You just have to add <modelVersion>4.0.0</modelVersion> to the project element.
Notice that modelVersion contains 4.0.0. That is currently the only supported POM version for both Maven 2 & 3, and is always required.
Usally the examples from plugin pages are only partial because you most likely already have a pom file and want to add the plugin. In the linked example this is indicated by ..., so you can't copy a paste the whole thing.

eclipse : gwt +maven +hibernate

I am really baffled.
I've been fighting against hibernate for a long time now and with no success passing run time. I got runtime exceptions when I tried it running it alone.
Then I decided I should try integrating it with gwt(the original project was a gwt without hibernate) and maven.
I have gwt and hibernate plugins installed via update site
and maven for eclipse wtp via marketplace
I've done a right click and converted the gwt+hibernate project into a maven project
now I get the following error
The output directory for the project should be set to /BGUSched/war/WEB-INF/classes
this is my auto generated pom.xml (located in top level directory - BGUSched)
<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>BGUSched</groupId>
<artifactId>BGUSched</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<testSourceDirectory>test</testSourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
my classpath:
-hibernate (as user library):
-antlr-2.7.6.jar
-commons-collections-3.1.jar
-dom4j-1.6.1.jar
-hibernate3.jar
-hibernate-jpa-2.0-api-1.0.0.Final.jar
-javassist-3.9.0.GA.jar
-slf4j-api-1.5.8.jar
-slf4j-simple-1.5.8.jar
gwt:
-appengine-api-1.0-sdk-1.6.3
-appengine-api-1.0-sdk-1.6.4
-appengine-api-1.0-sdk-1.7.0
-appengine-api-labs-1.6.3
-appengine-api-labs-1.6.4
-appengine-jsr107cache-1.6.3
-appengine-jsr107cache-1.6.4
-appengine-local-runtime-shared
-appengine-tools-api
-BGUSched.jar (my project's jar)
-commons-codec-1.3
-commons-fileupload-1.2.2
-commons-io-2.1
-commons-logging-1.03
-datanucleus-appengine-1.0.10.final
-datanucleus-core-1.1.5
-datanucleus-jpa-1.1.5
-el-api
-facebook4gwt-1.0.8
-geronimo-jpa_3.0_spec-1.1.1
-geronimo-jta_1.1_spec-1.1.1
-guice-2.0
-gwt-cal-0.9.3 (some other project embedded in mine)
-gwt-dev
-gwt-dnd-3.1.2
-gwt-log-3.1.8-javadoc
-gwt-plus-v1-0.2-alpha
-gwt-servlet
-gwt-user
-httpclient-4.0
-httpcore-4.0.1
-jdo2-api-2.3-eb
-JOpenId-1.08-sources
-jsp-api
-jsr107cache-1.1
-nekohtml-1.9.14
-openid4java-0.9.6
-repackaged-appengine-ant-1.7.1
-repackaged-appengine-ant-launcher-1.7.1
-repackaged-appengine-jasper-6.0.29
repackaged-appengine-jasper-el-6.0.29
repackaged-appengine-tomcat-juli-6.0.29
servlet-api
spring4gwt-0.0.1
validation-api-1.0.0.GA
validation-api-1.0.0.GA-sources
xercesImpl-2.8.1
GWT SDK (2.5.0)
gwt-user
gwt-dev
validation-api-1.0.0.GA-sources
validation-api-1.0.0.GA
other jars:
junit
mysql-connector-java-5.1.6-bin
ojdbc5
hope i'm not making a mess here and that you guys can help me out
Just right click on your project - Navigate to Build Path - Configure -> Java Build Path -> Source Tab -> Set up Default Output folder to BGUSched/war/WEB-INF/classes
You can use the archeotype from:
Mojo gwt-maven-plugin
then add in the pom.xml the Hibernate dependencies.

Categories

Resources