Put libraries for spring boot application in extra lib dir - java

I am currently building my spring boot application as a fat jar. But because I am also building a docker image from these, I have to pull the jar with all its dependencies every time I change some of my code. So my question is: How would I have to change my gradle files to get a task called 'buildWithExternalLibs' that has all dependencies in a lib folder, but can still be started using java -jar ... as long as the lib folder is right next to the jar?

I have done it in maven project. You can use same logic in gradle too.
When you will clean install, it will copy your jar file too.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<executions>
<execution>
<phase>install</phase>
<id>install-jar-lib</id>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<groupId>com.***.*</groupId>
<artifactId>abc-library</artifactId>
<version>*.*.*</version>
<packaging>jar</packaging>
<file>${project.basedir}/src/lib/***.jar</file>
</configuration>
</execution>
</executions>
<inherited>false</inherited>
</plugin>

Related

why is 'dependency:copy-dependencies' executed unexpectedly during tycho build?

I want to add JARs to a plugin. Previously, I added them by hand but thought it would be a good idea to add them via dependency definitions.
I created a pom.xml in the bundle and added an execution:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.4.0</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>process-sources</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.basedir}/lib</outputDirectory>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
As soon as I save the file the dependency JARs are all copied to the lib folder. So far so good...
When I start a tycho build, such as mvn install, the build succeeds but the lib folder now contains a new folder lib/myplugin-1.0.0.qualifier-lib and this folder contains copies of all the JARs in lib but they all have the extension .jar.jar.
If I remove the copy-dependencies execution before starting the tycho build then this folder is not created.
Can anyone explain why this is happening and what I should do to avoid this behaviour?

Maven module for static files

I have a web application with spring and maven. The application is divided into maven modules. I have an "application-web" module that generates war. Another "application-ear" module that generates an ear. And another "application-static" with css, js and images that generates a zip file.
This would be the outline of my application:
application
application-web (java code)
application-ear
application-static (css, js and images)
application-resources (language properties)
I want to deploy the eclipse use files of "static" module, instead of using the files in the "application/src/main/webapp" directory. How can I do this? It is possible?
Don't waste your time with unpacking resources, everything works out of the box:
Resource bundles: the are always loaded from the classpath, regardlessly it is exploded (WEB-INF/classes) or compressed (WEB-INF/lib)
Serving static resources: bundle them up in a JAR and use either Tomcat (Servlet 3.0) feature to serve from META-INF/resources, see here or use Spring's built in mvc:resources element.
Simply add the dependency snippets in your WAR POM and your are done.
You can use the maven dependency plugin to unpack your static resources included in the zip file using the following:
In your application-web.pom
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack-resources</id>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<outputDirectory>resources</outputDirectory>
<includeArtifactIds>application-static</includeArtifactIds>
<includeGroupIds>your group id</includeGroupIds>
<includes>**/*.js,**/*.css</includes>
</configuration>
</execution>
</executions>
</plugin
This work:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>unpack-dependencies</id>
<phase>package</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<excludeTypes>pom</excludeTypes>
<includeArtifactIds>application-static</includeArtifactIds>
<includeGroupIds>com.foo.foo.application</includeGroupIds>
<outputDirectory>src/main/webapp</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<finalName>${project.artifactId}-${project.version}</finalName>
</build>
But I have another problem, with this solution only copy the static files when I execute clean package. If I run the application-web with eclipse and I do changes in a js file in application-static, the changes have no effect until I execute clean package. Any solution for this?

Maven plugin - edit files in target (war)

I am developing some little maven plugin and I need to edit some css and js files from target (not from src!). And I can't understand on what phase I can do it.
To get access to src I use the phases:generate-resources and the following code:
MavenProject project = (MavenProject) getPluginContext().get("project");
String projectDir=project.getBasedir().toString();
How can I get target when all js,css files are copied there but war file is not generated in order to edit some files from target and get final war with some modifications of js and css files?
EDIT
What for I need it. I have js files in my project: a.js, b.js. I want to obfuscate them via maven. I mean, obfuscate when I build project. And of course all files in final war must be obfuscated but the same files in src must be left unobfuscated.
Besides, I need to combine some obfuscated files into one file.
I found the answer. The problem is that we must add some logic between "prepare-package" and "package" phases. As we user maven-war-plugin we can do it using exploded goal. From official docs:
Create an exploded webapp in a specified directory.
And here it's necessary to remember one important thing that maven after version 2.0.1 copies resources twice so if we want to use maven 2.5 we must use <useCache>true</useCache>. So final solution:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>prepare-war</id>
<phase>prepare-package</phase>
<goals>
<goal>exploded</goal>
</goals>
</execution>
</executions>
<configuration>
<useCache>true</useCache>
</configuration>
</plugin>
<plugin>
<groupId>my plugin</groupId>
<artifactId>...</artifactId>
<version>....</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>...</goal>
</goals>
</execution>
</executions>
</plugin>

How do I create and run an executable jar with maven that has external dependencies?

I have a jar plugin but it's not running because it isn't including the external dependencies in the jar. I can't seem to figure out how to include these dependencies, I seem to be finding a bunch of different solutions that conflict with each other for some reason. I ideally would like it to run on systems without the need for any special maven commands.
create maven pom.xml with
<packaging>jar</packaging>
By default it should not pack into your jar all dependent libraries.
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
What you are looking for is to build an über-jar. The Maven Shade plugin can do that - http://maven.apache.org/plugins/maven-shade-plugin/. It even allows for renaming of classes.

Maven dependencies not being compiled

I have been trying for the last hour or so to get my Maven project to include source files from its dependencies, but for some reason, it isn't. I have followed the steps provided by the following link, but when I compile and run the plugin, I get a ClassNotFoundException:
https://github.com/mkremins/fanciful
I have made sure to include the dependencies and the repository from the link above into my pom.xml file, but when I compile, they don't get added to my .jar file.
I am fairly new to using Maven, and like it so far, albeit that it can be a pain to solve issues like this.
I am building the project by doing the following:
Right click project -> Run As -> Maven Build -> Goal: clean install
EDIT -
With a little more searching around, I figured it wasn't as easy as I thought so. I added the following to my pom.xml build section:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<minimizeJar>true</minimizeJar>
<artifactSet>
<includes>
<include>mkremins:fanciful</include>
<include>org.json:json</include>
</includes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
The only problem with this is that I needed to also manually include the dependencies of the main library I wanted to use - mkremins:fanciful; is there flag or option to automatically copy dependencies from the one file I need, rather than also including <include>org.json:json</include>?
Well, if you want to have your dependencies copied to your target jar, you need to tell maven to do so! Maven doesn't know if the artifact of your project is meant to be self-sufficient executable jar, jar to be executed inside a container or just a dependency or library for another project.
You might want to use copy-dependencies task from maven-dependency-plugin
For example:
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<includeScope>runtime</includeScope>
<outputDirectory>${project.build.directory}</outputDirectory>
<excludeTransitive>false</excludeTransitive>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
For more tweaking you might also want to play with jar plugin and assembly plugin. On more about creating executable jars:
http://www.ibm.com/developerworks/java/library/j-5things13/index.html?ca=dat-
You have mistaken the idea of Maven. Maven is intended to use dependencies which are located in Maven Central. It's idea is not to compile dependencies. I recommend you to read about Maven and learn how it works.

Categories

Resources