I have a .java file that makes use of JSON library, it imports the library's package:
However the package isn't recognised, so I tried to add the library to my project:
But it didn't work:
My library is located inside Root/libs/LibraryFolder1/LibraryFolder2/.java files and .classe files and .jar file
I tried importing both .jar and .java and library as a whole (by clicking on the root folder). Neither worked.
EDIT: I've also tried running a command:
mvn install:install-file -Dfile=C:\xampp\tomcat\webapps\Root\libs\LibraryFolder1\LibraryFolder2\Craps.jar -DgroupId=org.json -DartifactId=json -Dpackaging=jar -Dversion=20150912 -DgeneratePom=true
which gives a BUILD SUCCESS result, but doesn't seem to change anything.
Since you are using Maven as your dependency (aka libraries) management, you cannot add it in your ide and expect it to work. It will be overriten to what you have in your POM file when you synchronize the project. Moreover, if you would make this to work from IDE, compilation would fail on CLI so bye bye every auto build tools.
To make this work, you mas add your local JAR to your local Maven repository and include it trough POM file.
https://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html
After you install your Craps.jar as Maven artifact, include it in dependencies section in POM file.
Related
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.
So I need to connect a library to my repo in IntelliJ, so I can test the library as it interacts with my main project. And I got this command from a member on my team: "So you need to change .java files in entity manager (which is the library) repo and do mvn clean package to generate the jar file locally, the use that jar in your project repo.
Then create dummy folder in your project with the same package name and copy those .java files from entity manager."
What does change the java files mean? What about "use that jar"? I'm so confused.
Use "mvn install" instead of package. This puts the library into your local .m2 repo and makes it available to your project in idea
If you do mvn clean package it produces a .jar file in target/ folder. Use that dependency to your project if they both share a common parent pom.xml
If you do mvn clean install it produces the jar file in target/ folder + copies the .jar file to your local m2 folder. That way you can use that .jar file to any project locally. Just use the with matching version, artifact and groupId whereever you need to use that jar file.
For both options above, you need to define sth like below in your pom.xml
<dependency>
<groupId>com.my.library</groupId>
<artifactId>mylib-artifact</artifactId>
<version>1.VERSION</version>
</dependency>
Not recommended:
You can also put the .jar file to your project (without doing mvn clean install) See: http://blog.gtiwari333.com/2016/12/maven-use-local-jar-without-installing.html
Or manually add the .jar to your project as project dependency using your IDE's project configuration
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:
Below is an example, when IntelliJ does not recognize SLF4J library, which is inluded into the project:
What can be the reason of this?
I am assuming its a maven project and you had included sl4j dependency in your pom.xml.
You can try doing below things.
Can you open your project structure by pressing f4 key and see the Library section and check if sl4j jar is there, and if its not included there, then add it again. you can check the problem section as well and there should not be any error.
try deleting the target folder and run the mvn clean package command.
if it doesn't work then check if you have this jar in your .m2 folder with the correct version.if its not there then rerun the mvn clean package and see if it download the correct version of jar in .m2 folder.
let me know if this solve your issue or you have any questions.
I have added to my pom.xml a section that specifies the mainClass and allows it to essentially create an executable jar. I have included a bunch of dependencies that maven manages as well. It executes fine, but fails to run when it gets to a section of code that needs to know the location of a jar package that was made inhouse by somebody (i.e., not from Maven). In the project in eclipse I had put the jar in src/lib and my code is in src/main/java. I had to select properties and Java Build Path and specify there the src/lib location for the jar to get it to even compile. However, trying to run java -jar name.jar has it fail and complain because it fails to import the classes from the src/lib jar. Since it is not a maven thing, how to I make sure this is a dependency for this project and that it is seen on the project's classpath?
The thing with maven is that maven has to control all of the dependencies and that includes this jar you want to reference. That doesn't mean that you have to build that other jar using maven, you could mvn install it in your local repository or use a tool like Artifactory to put it in a private remote repository. I know that installing regular jars via Artifactory creates a pom file for the jar and from then on you can treat the jar like any other maven dependency.