SBT not adding unmanaged JARs - java

Hey I am trying to sbt compile a Scala project with 2 external .jar libraries as dependencies. I've added them in the /lib folder and I can see they are recognized by sbt when I run "show unmanagedClasspath" I see the 2 jars. However when I run "sbt assembly", sbt tries to resolve dependencies and fails when it can't download the libraries that are already there.

You don't need to add the jars that you've in the \lib directory again in libraryDependencies. libraryDependencies is part of your managedClasspath and everything in lib directory is part of your unmanagedClasspath, both together forms your dependencyClasspath.
You can see this by typing this on sbt prompt and checking the dependencies section:
inspect externalDependencyClasspath
In this case 'external' just means dependencies that are external to the project (ie not internal project dependencies)

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.

Idea intellij doesn't import library [pictures included]

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.

how does a Java program find its Maven packages?

I have a Java program in IntelliJ which has a pom.xml and uses Maven. The packages were downloaded and currently they are found by IntelliJ.
I'm a little confused though because the Maven repository is not part of the CLASSPATH as far as I can tell. So does IntelliJ just do a bit of magic where it looks into its Maven repository to find the packages? (I think that IntelliJ has its own Maven repo. I separately have Maven 3 installed, but I think it isn't using it.)
But more generally: If you build a JAR using Maven then I guess it will put the dependencies in the JAR where the Java program can find them, so there won't be a problem. But if you just run a Java program directly, do you need to add the Maven repository to your classpath or does something else happen?
Thanks for any information you can provide to lessen my confusion :)
When you start the program from IntelliJ using a runtime configuration for your main() method IntelliJ constructs the classpath from all the project dependencies. You can see this in the Run window, the first log line is the java command used to start the main(). It's a long line but it usually looks similar to:
java -javaagent:/opt/idea/idea-IC-173.3727.127/lib/idea_rt.jar=40165:/opt/infra/idea/idea-IC-173.3727.127/bin -Dfile.encoding=UTF-8 -classpath /home/ [...]
IntelliJ constructs the -classpath argument adding both the module target directory and the Maven dependencies referenced from the local Maven repository.
When you package the project using Maven mvn clean package usually it becomes a standalone JAR with just your code (unless you changed the defaults). Now you have a few choices how to provide dependencies needed to start your main():
Provide them using -classpath parameter just like IntelliJ.
Add maven-shade-plugin and use shade goal to the build a runnable Uber JAR. This creates a fat JAR which doesn't require -classpath.
Use some other Maven plugin to perform point 2 e.g. Spring Boot spring-boot:repackage goal.
All the required dependencies, defined in the pom.xml file(s), are downloaded from Maven Central (or others if configured) to the local Maven repository. That repository is located at <user home>/.m2/repository.
Maven generates/calculates a dependency tree to know all the required dependencies for the project. (you can also dump that tree with the command mvn dependency:tree. I always pipe the result to a file, because the tree can be large mvn dependency:tree > deptree.txt). Maven put them all on the classpath when executing a maven command like mvn compile
IntelliJ also use/calculate the dependency tree and add all the jar files to the projects classpath (point to the files in the <user home>/.m2/repository folder). You can see them all in the list with External Libraries, and they will be used / on the classpath for compilation and running the application.
When building a JAR file the dependencies are NOT added to the JAR. Only the bytecode (java classes) and resources from your own project are packaged into the JAR file. (Source files can also be packaged if you configure that)
By adding a Maven plugin (maven-shade-plugin) you can configure your project to also pack dependencies into the JAR. SpringBoot projects also will do that.

project will treat maven dependency as a folder instead of a jar file and on run time gives ClassNotFoundError

In my maven project, I have this dependency which is my own project that is installed in my local maven (through cmd mvn install):
<dependency>
<groupId>com.myproject</groupId>
<artifactId>api-core</artifactId>
<version>1.0.0</version>
</dependency>
However when I start the tomcat server, I will get ClassDefNotFoundError on the classes within this project.
I tried to add the jar to the deployment assembly through the project's properties, however when I add this jar file, it is taken as a folder and will always be placed into the Deploy path of "WEB-INF/lib/core.api.1.0.0.jar" which is then a folder inside the lib, therefore tomcat isn't able to locate the jar file. I also noticed in the Web Deployment Assembly, the Maven Dependencies are deployed to WEB-INF/lib. Apparently my own jar file is not considered Maven Dependencies when it is being deployed. When I further look into the Maven Dependencies from Eclipse, the jar file is packed inside as "core" folder and it is not treated as a jar file. Therefore on run time, the web app has trouble locating the jar file and is giving me complain.
Further investigation shows that in Eclipse, I have the core project imported, and Eclipse is "smart" to recognize that project is the one generating the dependency, and therefore automatically convert the jar to the folder. If I remove the core project, the maven dependencies will then successfully added as a jar file, and then the deployment to tomcat issue not problem at all!
So, my question is, is it a way to keep the dependency in folder structure, while I can still have to core project imported to my workspace?
Yes, there is one: Show up the contextual menu of the web project, go to the Maven tab and uncheck the Resolve dependencies from workspace projects option: In this way, Eclipse will not interfere in Maven's dependency resolution chain.

How to add a jar package to project in eclipse?

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.

Categories