Are dependencies themselves compiled when my project is compiled? - java

When I compile a Java project, are the dependencies of the project themselves compiled at that point? Or, are dependencies using pre-compiled Java class files?

Only the code you have locally is compiled, when you compile.
Code you reference is already available in compiled form, in a jar.

They're resolved. If the dependencies are part of the same Maven project or are imported into a managed IDE workspace, then they will be compiled and then placed on the classpath as part of your build. Most of the time, however, they will be downloaded as jars from some repository. (Note that dependency jars necessarily contain .class files; they might consist solely of resource files instead, such as WebJars.)

Related

Gradle - adding sources jar as a dependency

Gradle 5.6.4. I have source code of the library I want to add as a dependency to my project. I already know that I can compile it into jar with .class files and then write dependencies{ compile files('libs/mylibrary-compiled.jar') }, but I do not want to lose comments, variable names, etc. Sadly writing the same thing for a jar file consisting of .java files does not work - packages are imported, but classes are not.
I think copy-pasting the sources right into src/main/java directory is an option too but I want to keep my code separate from libraries... So is it possible with gradle?

Intellij not showing source of dependencies but always showing decompiled classes from jars in .m2

This issue started recently with upgrade of IntelliJ to 2021.1.2. I have multiple java maven modules in one of my project in IntelliJ. Some of the maven modules depends on other modules. When I want to view implementations of any api e.g. (Option+Command+B), it opens the decompiled .class files from the jar in my .m2 directory. Even though source of class is right there in my project. Earlier if I have imported source code of dependency module, then it will open java source file. If I have not imported dependency, only then it used to open decompiled class.
Any help appreciated, what to do so that IntelliJ first check existing source in project. and then open .java files from source instead of decompiled .class file from .m2 directory.

Gradle include compiled Java classes as a module dependency

I have been given a directory of compiled .class files in Java, which I must include as a dependency when building my JAR. I am building this JAR using Gradle. How do I include this directory as a dependency in Gradle?
Note: This is a directory that looks like 'com.example.projectname' with files like 'file1.class', 'file2.class', etc. I want these .class files to be in my JAR.
You can simply add any files to the JAR file of your project by configuring the respective jar task (created by the java plugin) :
jar {
from files('path/to/file.class', 'path/to/otherfile.class'
// or
from fileTree('path/to/dir') {
include '**/*.class'
}
}
Please note, that those classes are part of the classpath when using the JAR, but they won't be available when using an IDE, as Gradle does not know these files in its own understanding of either source sets (for compilation) or configurations (for dependencies).

ant to maven migration maven - jar building

I am migrating an Ant project to Maven. There are a lot of Java projects in "Ant solution" which are only compiled to a specific directory (compiled to *.class files and copied to build folder). I.e. some project A uses dependencies from project B, which are preceded by copying *.class files from output folder B to resource folder project A.
Are there any possibilities to create a Maven jar-Module which uses another jar-Module only for compilation? Without using <dependency>?
Are there any possibilities to create a Maven jar-Module which uses another jar-Module only for compilation?
Yes, see below
Without using <dependency>?
If you are using another module in your project, then you have a dependency on that project and should be declared as such. If you have a scope of provided, then the project which you depend on will only be used when compiling your project but will not be included in the final jar as it expects the classes it depends on to be provided at runtime.
Not entirely sure if I understood your question correctly as I do not see why you would want to not use the <dependency> section when you state in your question that you actually have dependencies.
http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

Project Build classes do not include external project imports

I have a project which imports another projects to its build path. When I clean the project to compile classes, the classes imported from external projects are not showing up as part of the compiled classes directory.
Do anyone know how to make sure these external import classes gets compile and included into my build classes?
Thanks
I don't think it's possible. But when you deploy your project, you'll certainly want to create a jar file to hold all your classes. And Eclipse has a wizard to export a jar file and choose to embed dependencies in the jar.
The usual way, however, is to have each project generate its own jar, and to use all the jar of the project + the jars of the dependencies as the classpath of the deployed project.
#Bitmap,
is this a WAR or EAR project?
In EAR project you have to specify clearly the "JAVA EE Module dependency" to include the referenced projects.
If you include a project as a build dependency, it will be for compile-time only.
If these are simple java projects you may want to look at "JAR JAR" enter link description here
to achieve this.
HTH

Categories

Resources