Generate sources from a local jar file in Maven - java

I have a locally stored jar file that I believe I need to generate the sources in order to consume it in my project.
I am using JDK 8, IntelliJ and Maven 3.5.2
I have installed the jar file into my local .m2 folder.
Perhaps I am missing a goal in my maven lifecycle or a plugin?
Not had to use a local jar dependency before so any help appreciated (I'm a tester not a developer)
I've attached a screenshot (apologies for all the obfuscation of package names that I feel might be commercially sensitive - possibly overkill!)
Note the red underlining in the src test....folders where the classes cannot access those within the jar dependency.
I have added the dependency within the pom.xml file also and it shows no errors.
I feel I should have run something like -Dsources=target/mycool-project-3.0.17-SNAPSHOT-sources.jar when using the mvn install:install-file command (which I used originally when installing to my .m2. However I tried this with no success.
Or maybe I need to mark the resources as a root within IntelliJ
Currently in Project Structure -> Libraries I see:

Related

Java Maven -> quickls add folder of jars as depencency

my Java Project uses a "/libs" folder containing ~100 .jar files. Almost all of them are not in an official maven repository.
1.) In the moment I manually added to whole folder to the classpath with my Eclipse IDE. That enables to compile and run the App using the Eclipse IDE. But if I want to maven to compile and create jar-with-dependencies, maven of course does not know about the "/libs" folder.
2.) I know that I can add a jar file to my local maven repo with mvn install:install-file but this would take a very long time because I would also have to open every jar and find the whole package name to insert as '-DgroupId' and the Name of the Main Class to add as '-DartifactId'
3.) My Questions:
3.1) Is there an easy way to let maven just include all jars in a folder like I did with my Eclipse IDE? I know that would break the principle of maven that every jar is identified with group and artifact id, but it would be a quick solution.
3.2) If it is not possible to add a folder with jars as a dependency in maven, is there a faster way to add a jar file into a local repo. It would be easier if there is a maven command where groupId and artifactId are automatically discovered by the jar that I do not have to open every jar file and find the Main Class and its classpath
Quick answer: No.
In the past, I have written a script for that because there is not support in Maven for this.

Given all libraries extracted from a Fat Jar, how to rebuild the source code?

There is a Fat Jar, built with Spring Boot and Maven. Its internal structure can be explored, using Java Decompiler. Notice the "lib" node, which is supposed to contain all libraries needed to run the Fat Jar.
The Fat Jar can execute successfully under JDK/JRE 1.8 as a standalone application. Using 7z, one can extract the Fat Jar and collect all libraries embedded in it.
I have the source code (Maven with POM.XML) that was used to build the Fat Jar, and I would like to modify it (no new additional library added). Put it simply, comment a few line of code, then recompile it (it will be again a Fat Jar).
Question: The problem is: when recompiling the source code (command: mvn package), one of repository is no longer accessible from Internet. Is there a definite way (step by step) to recompile? In theory, this should be possible as we already have all needed libraries to run the Jar (thanks to "Fat Jar" feature).
My Attempt:
Extract the Fat Jar.
Using mvn install:install-file -Dfile=<path-to-file> to install one by one library into local Maven repository (c:\Users\[username]\.m2\repository\). Then later, I can execute mvn package against the main source code, with the assumption that all libraries needed are available in the local Maven repo; thus, no need to fetch it from Internet.
But I got a lot of problems, e.g. 'groupId' is missing.
You need to install:install-file the missing libraries to your local repository and set the GAV (groupId, artifactId, version) correctly when installing it.
Generally, I would also check if there is an easier way to solve your problem than patching the library.

How to download all possible maven dependencies so they are local

I work behind a very massive firewall that likes to hiccup on random connections, which makes all work with remote repositories a living nightmare for me!
I am looking to work with a project from Git (this one https://github.com/mrniko/netty-socketio) which heavily utilizes maven for downloading dependencies.
What I would like to do is on another terminal (such as http://cloud9.io or something) download all the maven dependencies so that the entire project can be run standalone.
I have already tried mvn clean install and then zipping up the source folder, but its actually not enough! I still get ClassNotFound related errors when I try to run the project locally in eclipse. And for the record, I did add the compiled *.class files in the build properties, so Eclipse knows where they are. It seems like there are some random classes that get generated dynamically which still aren't present (such as log4j -- and I really don't want to hunt each one down individually)
I am wondering if there is a fully thorough way to download all possible dependencies from maven and then either run a project 100% standalone, or create a local maven server from it?
I am running Java 7 on Eclipse Luna and I do have Maven installed on my windows 7 machine (though again it barely works on remote repositories). I also have a Cloud9 instance which I could use to run Maven commands, then zip up the results for local download.
When you execute mvn clean install, maven downloads all dependencies of currently built project to your local maven repository. This is usually located in
%USERPROFILE%\.m2\repository
When you build your project, maven uses that path, to lookup required dependencies.
If you want do download them all, you can try using mvn dependency:copy-dependencies. Then, you'll find all project dependencies intarget/dependencies directory of your project. This also includes transitive dependencies.
To add them all as eclipse dependencies, you may want to try maven-eclipse-plugin. Using that plugin, you can generate eclipse .project and .classpath files, using mvn eclipse:eclipse command. This will populate eclipse files with required dependencies from maven. You should then import the project to eclipse using Import existing projects into workspace, instead of Import existing maven projects.
maven-eclipse-plugin will add all those jars relative to a folder specified by M2_REPO variable. Just make sure you edit this variable inside eclipse project properties, and you should be set.
I've had to deal with similar issues. I would find that due to changes in firewall policies, occasionally all the .jar files in my project had been updated to be a 1K file that, when opened within notepad++ contained a message from the firewall saying that the download had been blocked.
I recommend looking into Nexus for your local repository management, it means your local projects don't have to go past your firewalls to check for maven updates.
http://www.andrejkoelewijn.com/blog/2010/03/09/getting-started-with-nexus-maven-repository-manager/
Use dependency plugin go-offline task.

Project is missing required library [Eclipse]

I have imported a java project into a new eclipse workspace and it gave me a lot of errors:
It says "Project 'myProject' is missing required library: /User/linus/.m2/..." (The directory is longer but not of interest for you)
Now, I have looked inside my Finder to see what is in ".m2" but there is no such directory where it should be. Additionally I let it show all the hidden files but no success. Lastly I tried to get into the folder with the terminal (using cd and then the directory eclipse gave me) but that did not work either.
I saw this post but it did not help me.
I reinstalled Maven to make sure it can be used, this is the output if I type mvn --version and mvn in the terminal:
Does anyone have an idea?
Thanks in advance
EDIT:
I reinstalled Maven and now there is a /.m2 folder. It contains /.m2/repository/ but there is nothing in it...
May I suggest you read a Maven tutorial like http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html
IMHO it's a wonderful build tool because it gets required jar files per the specification in the project object model ("pom") file which your Maven-ized project should have, and you don't have to keep track of jars on your own.
Look in your project for pom.xml, you might even post it here.
To check the sanity of the project and maven on your computer (ignoring Eclipse for a moment), be sure you have a good Internet connection, open a Terminal window, change directory ("cd") over to the project directory, and type "mvn install".
This should trigger the usual Maven build cycle. One of the first things it should do is download all the jars as identified in the pom.xml and store them deeply nested within your /Users/linus/.m2/ directory. Then it should compile all your java classes. Eventually it will probably build a jar file and copy it to somewhere.
If all this works, visit http://eclipse.org/m2e/ for directions to add the Maven plugin (m2e) to your Eclipse. Then your Eclipse should understand how to download jars and build the project per the pom.xml file.
Eventually this all should fix the original "Project is missing required library" problem.

Import and use a Maven managed Java library (CitySDK)

I am trying to build a web application based on the project library CitySDK, but i don't know how to import it properly into my own project.
I have created a Maven Web Application and added the CitySDK library as a Dependency, and then Selected the Project, right-clicked and Selected Build with Dependencies. No errors appeared, and in the image below you can see it appeared as a dependency.
However, whenever i try to use some of the classes specific to it, in my own Web Application(called TourismApplication's TestClass.java), the compiler displays an error, that the package is unknown. Could anyone suggest ideas as to what i have missed? I have followed a few Maven tutorials, but without any effects.
The dependencies are as follows:
Ok I did some digging and I think I figured out your problem. Their POM file is incorrect based on the structure of project.
First off, I'll paraphrase what I think the steps you took to get it built in your project were, to ensure I followed the same steps to get it working. These are the steps I took:
Cloned/downloaded the sources from the link you placed in the OP
Built the project into a jar file by running the command they said to use: mvn clean package assembly:single
Installed the artifact in your local Maven repo using mvn install
Added a dependency in your project POM
I tried the same thing you did, using the library in a test source file, to no avail. I looked at the .jar file that was built using their instructions and didn't find any .class files archived into it... it was essentially empty.
Turns out, their src folder structure follows Maven standards, but their POM file indicates the sources are down a different chain of directories. If you examine the build log closely, you see "[INFO] No sources to compile"
The POM.xml file they provide specifies the source directory as
<sourceDirectory>src/citysdk/tourism/client</sourceDirectory>
However, the actual files are at
src\main\java\citysdk\tourism\client
After changing the line in the POM file from the above to (similarly for tests):
<sourceDirectory>src/main/java/citysdk/tourism/client</sourceDirectory>
<testSourceDirectory>src/test/java/citysdk/tourism/client/tests</testSourceDirectory>
rebuilt, and installed, it worked when I tried to use it in my project. Hope this helps.
FYI, I used IntelliJ as my IDE, but it should work the same with Eclipse.

Categories