How to access file in a JAR when running a Maven build - java

I want to run a liquibase update by the liquibase-maven-plugin, but the changeset yml file is inside a .jar that I pull in by dependency.
When I open the .jar with TotalCommander, it has the following structure:
xy.jar/changelog/changeset.yml.
I tried including it as a resource directory, but it failed telling me the .jar is not a directory.
I also tried building to a WAR and just setting the liquibase changeLogFile property to the path leading to the .jar, like {projectDir}/target/..war/..lib/..jar/changelog/myFile.yml
But maven could not find it this way.
Is it possible to access this file somehow?

You can read the file using below code.
InputStream inputStream = getClass().getClassLoader().getResourceAsStream("changelog/myFile.yml");

Thanks for the reply, but the solution was simpler, I'm just rusty in this area :) I simply had to add the required JAR from the WAR as a Maven Dependency. This adds the JAR's content to the classpath in the Maven build process, so I can reference to it's files as they were on the project root path.
TLDR: add JAR as dependency in the pom and use the path:
<changeLogFile>changelog/xy.jar</changeLogFile>

Related

IntelliJ Build Artifact causes executable .jar to ignore Class-Path setting

When I create an executable .jar file using IntelliJ, the .jar file always ignores it's own class-path in the manifest.mf file.
Thanks to this question: Executable JAR ignores its own Class-Path attribute I have been able to find that the cause of the problem is that during the creation of the .jar file, a META-INF/LIST.MF file is created. If I delete that file, everything works fine. However, unlike the question linked, my pom.xml file does not contain the command to create this file.
How do I turn this off in IntelliJ?
Try, when creating an artifact in IntelliJ,project structure -> Artifacts -> new from maven with dependencies, in the path for Directory for the META-INF/MANIFEST.MF, always change the path to the sibling resources directory, it should not be set tojava` directory.

Add library into jar of project

My problem is with a project where I use the POI library. It's from apache and allows you to work with excel,word, ... .
I had to add some jar files to my library and it runs perfect.
The problem is when I put my JAR file somewhere else.
It gives me the errors it can't find the librarys.
Is there a way I can put my librarys into the 1 jar?
I already tried to add my lib folder as source package but that didn't help.
there is a Maven plugin for Eclipse called M2Eclipse, which will read a POM and construct a classpath out of jars it finds in the local repository and any remote repositories you've configured. It behaves largely like Maven does in terms of finding the latest version for a given jar (if you've specified a version range in your POM).
You can also have a look on this
http://fredpuls.com/site/softwaredevelopment/java/deploy/five_strategies_for_managing_j.htm
You want to make a 'shaded' or 'uber' jar, which has all of its dependencies included
There is a maven plugin for building a shaded jar.
https://maven.apache.org/plugins/maven-shade-plugin/
For me the fastest/ easiest solution was to just open my project in eclips and export is a runable JAR then it adds the librarys to the JAR.
NetBeans builds your application into the project's dist folder. There is also a readme file in it, that tells you, you should distribute the contents of that folder...
Edit
Remove the lib folder, that is shown on the second image, from your sources.

Ant Java Build Path set by Eclipse is Missing a Library at Compile Time

I'm using eclipse and ant to compile a java project. The ant compile script calls javac using a classpath refid of classpath, which is set based on System Property variable java.build.path.
My java.build.path variable is missing a library that was specified in
Project->Properties->Java Build Path->Libraries.
That is, the external jar was properly added to the list of libraries to add to the build path, is not missing or corrupt, and I have every expectation that eclipse would include the library in the build path. My build fails because java.build.path is missing this library.
Furthermore, the file <projectDir>/.classpath contains a valid classpathentry element for the missing jar file.
When building, javac fails at an import statement, claiming that the package does not exist. The value of java.build.classpath contains many of the libraries I set in the project properties, but does not include the missing library. Its as if the project property for that external jar was never set.
For what its worth, the missing library is jboss/lib/jbosssx.jar
Any help here would be appreciated.
At first,
Download jbosssx.jar jar file from this link
Then keep the jar file in the folder jboss/lib
Hope it will solve your issue.
UPDATE:
Sometimes jar files need to keep in lib folder physically. For this reason, you can put the jar file in your project lib folder.
I found where my configuration was messed up.
Using External Tools Configurations within eclipse, I found another Classpath settings tab for my ant build that did not contain my library.
Adding my library here caused the build to succeed!
Skywalker, thanks for the suggestion. :)

archive for required library can not be read

I created my first maven project and I get Java build path problem in it.
Description Resource Path Location Type
Archive for required library: 'C:/Users/simran/.m2/repository/org/glassfish/extras/glassfish-embedded-web/3.1.1/glassfish-embedded-web-3.1.1.jar' in project 'RestfulMessenger' cannot be read or is not a valid ZIP file RestfulMessenger Build path Build Path Problem
This jar is present in the location mentioned, I tried deleteing it from there , cleaning the project and updating the maven but nothing fixed the error.
What seems wrong?
You should try doing
Maven -> Update Project and make sure you check Force Update of Snapshots/Releases
Deleting the corresponding .jar files from local repository and reissuing the maven command did the trick. It seems the .jar files got corrupted while downloading.

Why a file always missing after build in Intellij with maven?

I have been stuck in the problem for quite a while.I use Intellij and maven not very long.
the problem like this:
the version of my Intellij is 12.1.4,I am making a project of Spring MVC with maven.So the output directory is 'target' in the project workspace.I have a mybatis xml file in my path.
once I found that the xml file did not change autoautomaticly in the target directory.so I delete it in the target directory.after I have done this,I rebuild the project,but the file never appear any more.
why?how should I do?
put that file in src/main/resources, maven copies all the resources from default resource directory to target/classes

Categories

Resources