Maven dependencies not being compiled - java

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.

Related

Gradle - Is there a possibility to compile a single Java class before the rest of the project?

I am migrating my project from Maven to Gradle and i have a Java Class that i need to have compiled before the rest of the project. This class creates a file that will be used by the project.
Does anyone have an idea how to accomplish this task? Thanks
Edit:
The following is the Maven solution:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>11</release>
</configuration>
<executions>
<execution>
<id>default-compile</id>
<configuration>
<compilerArgument>-proc:none</compilerArgument>
<includes>
<include>PATH/TO/CLASS/AnnotationProcessor.java</include>
</includes>
</configuration>
</execution>
<execution>
<id>compile-project</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
The AnnotationProcessor scrapes and stores all annotation data that the project declares. This class is supposed to be compiled before the rest of the project. At least that is what the comment section says.
Seems like you have some class that needs to exist before the rest of the project can be built. One way to resolve this would be to extract the dependency to a separate module so you have a multiproject build... This post may help you achieve what you want.
Build order of Maven multimodule project?
You can also introduce custom tasks in Gradle that will enable you to have "dependsOn(someTask)"
See https://docs.gradle.org/current/userguide/tutorial_using_tasks.html#sec:task_dependencies for more/examples

Put libraries for spring boot application in extra lib dir

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>

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.

How to include my *.jasper files together with *.class files in maven build?

I have a web project that was compiled by ANT. The *.jasper files were compiled with the *.class files. Once migrated to MAVEN they ceased to be integrated with the build.
I tried to include these file using the resource maven tag, but it seems that only include files that are in the resource. How do I tell maven to put them together (in the same folder) with the *.class?
You'll need a maven plugin for Jasper. I haven't used this myself but this should be exactly what you need
https://github.com/alexnederlof/Jasper-report-maven-plugin
Follow the usage instructions
<build>
<plugins>
<plugin>
<groupId>com.alexnederlof</groupId>
<artifactId>jasperreports-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<phase>process-sources</phase>
<goals>
<goal>jasper</goal>
</goals>
</execution>
</executions>
<configuration>
<outputDirectory>${project.build.directory}/jasper</outputDirectory>
</configuration>
</plugin>
</plugins>
And you should be good!
Update
Sounds like you have some config needs, to change your source directory
<configuration>
<sources>
<source>src/main/java/</source>
</sources>
<outputDirectory>(same as your java output)</outputDirectory>
</configuration>
I believe this will help you out!
I found the solution in this link: https://bowerstudios.com/node/976
In configuration tag, i put the tag:
<sourceDirectory>src/main</sourceDirectory>
And works fine!
Thanks a lot

how to reference native DLL from Maven repo?

If a JAR is accompanied with a native DLL in Maven repo what do I need to put into my pom.xml to get that DLL into the packaging?
To be more specific take for example Jacob library. How do you make jacob-1.14.3-x64.dll go into the WEB-INF/lib folder after you run mvn package?
In our local Nexus repository we've got these definitions for JAR and DLL:
<dependency>
<groupId>net.sf.jacob-project</groupId>
<artifactId>jacob</artifactId>
<version>1.16-M2</version>
</dependency>
<dependency>
<groupId>net.sf.jacob-project</groupId>
<artifactId>jacob</artifactId>
<version>1.16-M2</version>
<classifier>x64</classifier>
<type>dll</type>
</dependency>
But putting the same dependencies to our project POM and running mvn package doesn't make DLL go to WEB-INF/lib, but JAR gets there fine.
What are we doing wrong?
Thanks to the hint from Monty0018 I was able to solve the problem. The maven code that works for me:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<excludeTransitive>true</excludeTransitive>
<includeArtifactIds>jacob</includeArtifactIds>
<failOnMissingClassifierArtifact>true</failOnMissingClassifierArtifact>
<silent>false</silent>
<outputDirectory>target/APPNAME/WEB-INF/lib</outputDirectory>
<overWriteReleases>true</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
For a DLL, you will need to use the Copy Dependencies MOJO.
You can filter out all dependencies other than the DLL and specify anywhere in your project structure to copy them to, including your target/webapp/WEB-INF/lib.

Categories

Resources