Shrink my huge 100MB jar file (spring boot maven plugin) - maven

I want to build my spring boot app, but I have to transfer the files around a lot into different servers. But my "libs" folder inside my "jar" file, is too big now. It's 100MB++.
What I want is to periodically update my webapp, that is like 1MB or something, and keep a copy of my 100MB "libs" folder on the output directory at different servers. Only updating the libs rarely.
The following POM file makes a 100MB JAR... and then makes 100MB "libs" folder. Then zips the two together. But I don't know how to make 100MBJAR smaller (what springboot libs are most important to be compiled together?). I think I need an <include> tag or something.
But I haven't found the right way to configure "spring boot maven plugin" so that it only includes the most essential files from SpringBoot & TomCat Embedded, but keeps most of the other libraries outside in a different folder.
(bottom of my) POM.xml
<build>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<excludes>
<exclude>node_modules/**</exclude>
<exclude>bower_components/**</exclude>
<exclude>node/**</exclude>
</excludes>
</resource>
<resource>
<directory>${project.build.directory}/generated-resources</directory>
</resource>
</resources>
<pluginManagement>
<plugins>
<!--This plugin's configuration is used to store Eclipse m2e settings
only. It has no influence on the Maven build itself. -->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<projectNameTemplate>[groupId].[artifactId]</projectNameTemplate>
</configuration>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/${project.build.finalName}.lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>${project.build.finalName}.lib/</classpathPrefix>
<mainClass>${fully.qualified.main.class}</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>antrun-archive</id>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<property name="final.name" value="${project.build.directory}/${project.build.finalName}"/>
<property name="archive.includes" value="${project.build.finalName}.${project.packaging} ${project.build.finalName}.lib/*"/>
<property name="tar.destfile" value="${final.name}.tar"/>
<!-- <zip basedir="${project.build.directory}" destfile="${final.name}.zip" includes="${archive.includes}" /> -->
<tar basedir="${project.build.directory}" destfile="${tar.destfile}" includes="${archive.includes}" />
<gzip src="${tar.destfile}" destfile="${tar.destfile}.gz" />
<!-- <bzip2 src="${tar.destfile}" destfile="${tar.destfile}.bz2" /> -->
</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Added "excludeGroupIds" to spring-boot-maven-plugin and comma-separated list of groupIDs, and it worked. Removed it from the core jar.
Remember this is NON-standard, and shouldn't be done due to the fact that it defeats purpose of SpringBoot (So might as well use Java Spark2 Framework or something more modern, then configure Tomcat separately), or just send the 100MB package over. But this was necessary here because of slow connections.

Related

Maven: properties-file alongside jar

I am packaging my application as jar.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<mainClass>...</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
And also I have my .properties-file placed inside of src/main/resources. I can freely move this file to any other place. I don't want properties-file to be included into jar, but rather placed into the same output directory (where we get jar) side-by-side. What is the best practice for this in Maven?
Okay, one can use goal resources:copy-resources of maven-resources plugin.
I am just leaving standard resources folder for embedded (into jar) resources. And for external resource I create another folder: src/main/external-resources. Here I put my .properties-file.
Following piece will copy external resources to output dir upon validate phase. You can alter the phase per your own needs.
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}</outputDirectory>
<resources>
<resource>
<directory>src/main/external-resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>

maven-ejb-plugin: include generated sources

I have an EJB-maven-Project that has some generated classes (generated by JAXB).
They are generated into: target/generated-sources/jaxb/
Now, with maven-ejb-plugin I want them (i.e. their compilated classes) to be included into the client-jar, something like that:
<plugin>
<artifactId>maven-ejb-plugin</artifactId>
<version>2.3</version>
<configuration>
<!-- Tell Maven we are using EJB 3.1 -->
<ejbVersion>3.1</ejbVersion>
<generateClient>true</generateClient>
<clientIncludes>
<clientInclude>com/bla/ch/authorization/client/**</clientInclude>
<clientInclude>target/generated-sources/jaxb/**</clientInclude>
</clientIncludes>
</configuration>
</plugin>
This does not work, the generated classes are not part of the ejb-client-jar. (Though they are in the ejb-jar).
How can I do this correctly?
Include sources in your jar is probably not a good solution.
You may add generated sources in you resources locations, and then use source-plugin, to generate the so called artifact-sources.jar
<resources>
<resource>
<directory>${basedir}/target/generated-sources</directory>
<filtering>true</filtering>
</resource>
</resources>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
This is a better way than producing a jar with source code.

How can I generate Flash HTML wrappers in a WAR using Flexmojos and Maven 3?

I have two Maven projects: one to build a SWF and another one to build a WAR that contains it. The WAR needs to contain a Flash wrapper for the SWF. Flexmojos is apparently capable of doing this via the HTML wrapper mojo, but this doesn't work with Maven 3. See here for more details.
Is there a workaround for this?
My SWF POM has the following:
<plugin>
<groupId>org.sonatype.flexmojos</groupId>
<artifactId>flexmojos-maven-plugin</artifactId>
<version>4.1-beta</version>
<configuration>
<parameters>
<swf>${project.artifactId}-${project.version}</swf>
<title>My application title</title>
<width>100%</width>
<height>100%</height>
<bgcolor>#ffffff</bgcolor>
</parameters>
</configuration>
</plugin>
and my WAR POM has the following:
<plugin>
<groupId>org.sonatype.flexmojos</groupId>
<artifactId>flexmojos-maven-plugin</artifactId>
<version>4.1-beta</version>
<extensions>true</extensions>
<executions>
<execution>
<id>wrapper</id>
<phase>generate-resources</phase>
<goals>
<goal>wrapper</goal>
</goals>
<configuration>
<wrapperArtifact>
<groupId>${project.groupId}</groupId>
<artifactId>the-swc-project</artifactId>
<version>${project.version}</version>
</wrapperArtifact>
<htmlName>index</htmlName>
</configuration>
</execution>
<execution>
<goals>
<goal>copy-flex-resources</goal>
</goals>
</execution>
</executions>
</plugin>
Upon further investigation, I discovered that the problem here is that Flexmojos tries to reconfigure the WAR plugin, but it does so in a way that doesn't work with Maven 3.
The fairly grim workaround that I found involved manually configuring the WAR plugin as follows:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<warSourceExcludes>index.template.html</warSourceExcludes>
<webResources>
<resource>
<directory>target/war/work/wrapped-template</directory>
</resource>
</webResources>
</configuration>
</plugin>

Maven: JspC should use external JSP-files

We're using Maven 3 and I'm facing a project that has JSP-files and also uses "global" JSP-files stored in a different project. This works nicely when using maven-war-plugin and webResources. All JSP-files find their way into the WAR-file.
The new idea is to pre-compile all the JSPs. The obvious choice is to use jspc-maven-plugin. However, that doesn't include the external JSPs when it compiles the project-local JSPs.
Here's the snippet from the pom.xml:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jspc-maven-plugin</artifactId>
<executions>
<execution>
<id>jspc</id>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1</version>
<configuration>
<warName>${pom.groupId}.${pom.artifactId}-0.0.1-SNAPSHOT</warName>
<webXml>${basedir}/target/jspweb.xml</webXml>
<webResources>
<resource>
<directory>../name.of.external.project/src/global/webapp</directory>
</resource>
</webResources>
</configuration>
</plugin>
The error is
[ERROR] Failed to execute goal org.codehaus.mojo:jspc-maven-plugin:1.4.6:compile (jspc) on project internal.project: JSPC Error: file:C:/workspace/name.of.internal.project/src/main/webapp/WEB-INF/views/show.jsp(2,0) File "/WEB-INF/views/../jspGlobal/jsp-declaration.jspf" not found -> [Help 1]
The jspGlobal-directory would get copied with the <directory>../name.of.external.project/src/global/webapp</directory>-line above.
What's missing to include the external JSPs in JspC?
EDIT: Thanks to prunge's and Raghuram's input I looked deeper into sources and JavaDocs. I noticed that the mentioned sources requires a FileSet which does NOT allow a list of directories. And since sources is also not a list, I see no chance how I can specify more than one JSP-source directory. I even tried to copy the <plugin>-element, but that didn't help. My current situation is this:
<plugin>
<groupId>org.codehaus.mojo.jspc</groupId>
<artifactId>jspc-maven-plugin</artifactId>
<version>2.0-alpha-3</version>
<executions>
<execution>
<id>jspc</id>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<configuration>
<sources>
<directory>${basedir}/../name.of.external.project/src/global/webapp</directory>
</sources>
<!-- the later mentioned <sources> gets picked
<sources>
<directory>${basedir}/src/main/webapp</directory>
</sources>
-->
<!-- 1.6 doesn't work!? Something lower than 1.5 seems to be the default -->
<source>1.5</source>
<target>1.5</target>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.mojo.jspc</groupId>
<artifactId>jspc-compiler-tomcat6</artifactId>
<version>2.0-alpha-3</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1</version>
<configuration>
<warName>${pom.groupId}.${pom.artifactId}-0.0.1-SNAPSHOT</warName>
<webXml>${basedir}/target/jspweb.xml</webXml>
<webResources>
<resource>
<directory>../name.of.external.project/src/global/webapp</directory>
</resource>
</webResources>
</configuration>
</plugin>
Now the external JSPs are compiled into the target-path of the current project. Now I need a way to compile the JSPs of the current project. How do I do this?
BTW, if I switch the <sources> to the line for the current project I get the same error as mentioned earlier.
Perhaps you could try with the latest version of jspc-maven-plugin, which is 2.0-alpha-3. Do note that the usage is a little different from the earlier version.
Looking at the source code of CompilationMojoSupport, I see a sources property of type FileSet. You might be able to configure this in the configuration of the plugin to add additional source directories. It looks like by default it uses ${project.basedir}/src/main/webapp regardless of the configuration of the WAR plugin.

Maven: how to get a war package with resources copied in WEB-INF?

when I create a war package with maven, files and directories under the directory "src/main/resources" are copied in /WEB-INF/classes instead of /WEB-INF. How can I get them copied in /WEB-INF?
thanks,
rand
UPDATE:
in my pom now I use this:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>war</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>myapp/target/WEB-INF</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
and I launch mvn with:
mvn -Dmaven.test.skip=true clean package resources:copy-resources
but I got:
[INFO] One or more required plugin parameters are invalid/missing for 'resources:copy-resources'
[0] Inside the definition for plugin 'maven-resources-plugin' specify the following:
<configuration>
...
<outputDirectory>VALUE</outputDirectory>
</configuration>.
[1] Inside the definition for plugin 'maven-resources-plugin' specify the following:
<configuration>
...
<resources>VALUE</resources>
</configuration>.
I'm using maven 2.2 and the snippet basically is the same of the documentation
any idea?
either configure the outputDirectory parameter of resources:resources plugin, or put your files under src/main/webapp/WEB-INF/ directory.
resource plugin
EDIT:
This configuration is working for me:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.2</version>
<executions>
<execution>
<id>default-copy-resources</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<overwrite>true</overwrite>
<outputDirectory>${project.build.directory}/${project.artifactId}-${project.version}/WEB-INF/</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
you can run a phase in the form somePhase or a goal somePlugin:someGoal. The phase invocations will invoke all plugins goals hooked on phases in interval [validate,phase] in order, so there's no need to explicitly call them.
Web resources are not the same as java resources, which should be placed in the classpath. Web resources are processed via the war plugin and should be placed into src\main\webapp\WEB-INF\. In this case, it will work automatically without any additional configuration in the pom.xml
This configuration is working add plugin pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<webResources>
<!--copy resource file location-->
<resource>
<directory>${project.build.directory}/classes</directory>
</resource>
</webResources>
<!--location for add file-->
<webappDirectory>${project.build.directory}/${project.build.finalName}</webappDirectory>
</configuration>
</plugin>

Resources