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?
Related
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.)
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).
Edit:
How can I included packages in the .classpath that are downloaded at compile-time by gradlew and use them in Eclipse before compile-time such that my .classpath doesnt include local file paths from my personal computer?
I would like to use the org.apache.commons.cli package, but do so without altering the .classpath file. I'm using the gradlew to build my project, and in the build.gradle file is:
dependencies {
compile 'commons-cli:commons-cli:20040117.000000'
compile 'com.google.code.gson:gson:2.8.2'
testCompile "junit:junit:4.11"
}
So at compile-time, the necessary libraries are managed. However, without them added to Eclipse, using them seems to be impossible as they cannot be resolved.
Question:
If using build.gradle to manage project dependencies, is there a way to use them in Eclipse without going to the project's build path and adding external jar's? If not, what is the general practice for situations like this, where a library is needed to be use, but I dont want my local file path stored in the .classpath, where a user may not have the same jar in the same file path?
You should install and use Eclipse BuildShip to properly work with gradle projects.
Basically i want to compile module using maven jar plugin and to create folder structure, compiled java classes, manifest, etc. without packing them into an archieve. Is it possible to achieve?
I've been used to simply importing jar files into my netbeans projects and then being able to import them in my projects. For example, I included the guava.jar. I saw it under libraries and when I expanded it, I saw things like com.google.common.. So, I just said import com.google.common. in my own classes and I was able to use their methods. Similarly for commons-math3-3.0.jar. But now I similarly included joptimizer-3.2.0-sources.jar (http://www.joptimizer.com/usage.html). When I expand it, I see folders like "src.main.com.joptimizer.functions" However, this time when I go to my projects and try to say import src.main..., it doesn't work they are not recognized. Why did simply importing the jars work before, but not in this case? And what do I need to do in this case to be able to use the methods in the joptimizer jar?
You included sources to you project. Sources means jar with not compiled *.java files.
You should add jar with compiled java classes (*.class files).
Looks like there is no compiled library on http://www.joptimizer.com/usage.html site
and you should compile it manually.
Install maven tool, unpack joptimizer-3.2.0-sources.jar and execute
mvn package
in the root directory (directory with pom.xml)
You will find compiled library in target directory