Gradle import java class

How do I import a class in gradle outside of build.gradle in a apply from: file

I get an unable to resolve class error if I try to import a class outside of build.gradle, does anyone have any insight as to why that wouldn’t work or what the proper way of doing this?

Answers

Try to move the buildscript block into gradle/foo.gradle .

  1. Write down the words. You started to do this. Some people don’t and wonder why they have problems.
  2. Expand your set of words into simple statements about what these objects will be doing. That is to say, write down the various calculations you’ll be doing on these things. Your short list of 30 dogs, 24 measurements, 4 contacts, and several «parameters» per contact is interesting, but only part of the story. Your «locations of each paw» and «compare all the paws of the same dog to determine which contact belongs to which paw» are the next step in object design.
  3. Underline the nouns. Seriously. Some folks debate the value of this, but I find that for first-time OO developers it helps. Underline the nouns.
  4. Review the nouns. Generic nouns like «parameter» and «measurement» need to be replaced with specific, concrete nouns that apply to your problem in your problem domain. Specifics help clarify the problem. Generics simply elide details.
  5. For each noun («contact», «paw», «dog», etc.) write down the attributes of that noun and the actions in which that object engages. Don’t short-cut this. Every attribute. «Data Set contains 30 Dogs» for example is important.
  6. For each attribute, identify if this is a relationship to a defined noun, or some other kind of «primitive» or «atomic» data like a string or a float or something irreducible.
  7. For each action or operation, you have to identify which noun has the responsibility, and which nouns merely participate. It’s a question of «mutability». Some objects get updated, others don’t. Mutable objects must own total responsibility for their mutations.
  8. At this point, you can start to transform nouns into class definitions. Some collective nouns are lists, dictionaries, tuples, sets or namedtuples, and you don’t need to do very much work. Other classes are more complex, either because of complex derived data or because of some update/mutation which is performed.
Читайте также:  Python get last item in list

Don’t forget to test each class in isolation using unittest.

Also, there’s no law that says classes must be mutable. In your case, for example, you have almost no mutable data. What you have is derived data, created by transformation functions from the source dataset.

Using , as a field separator, take record number 4 and print field 3 in somefile.csv .

The following setup should work with Gradle 5.6 (when using another attribute it will likely also work with previous versions). It mostly corresponds to your original setup with the exception of the changes indicated by XXX .

Project testA

plugins < id 'base' // Uncomment the line below to break the zip artifact //id 'java' >group = 'org.test' version = '0.0.0.1_test' task zipTask(type: Zip) < from './settings.gradle' // just so the zip isn't empty >// XXX added an attribute to the configuration configurations.default.attributes < attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, project.objects.named(LibraryElements, 'my-zipped-lib')) >artifacts.add('default', zipTask) 

Project testB

rootProject.name = 'testB' // This line may be commented out in some cases and then the artifact should be downloaded from Maven repository. // For this question it should be always uncommented, though. includeBuild('../testA') 
plugins < id 'base' >configurations < // XXX added the same attribute as in the testA project myConfiguration < attributes < attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, project.objects.named(LibraryElements, 'my-zipped-lib')) >> > dependencies < myConfiguration 'org.test:testA:[email protected]' > task doubleZipTask(type: Zip)

I have tested this setup both with and without the java plugin. I’ve also tested publishing to a Maven repository and letting testB take its dependency from there instead of from the included build of testA . Adding an additional dependency from testB on the JAR artifact of testA ( myConfiguration ‘org.test:testA:[email protected]’ ) worked, too.

Читайте также:  Buy Local

Some explanation on what (I believe) is going on: Gradle needs a way of determining automatically which local component/artifact in testA it can use for substituting the external dependency of testB .

  • Without applying the java plugin, there is only a single component/artifact and my guess is that Gradle then just selects that single one without further ado.
  • As you saw, by applying the java plugin, another artifact is added to testA . Now which one should Gradle take? One would expect that it would look at the file extension specified on the dependency in testB but that doesn’t seem to be the case. It seems that Gradle isn’t actually working at the artifacts level when substituting dependencies but rather at the module/component level. You might say we only have one component with two artifacts, so selecting the one component should be straightforward. But it seems that we actually have two variants of the same component and Gradle wants to select one of these variants. In your own testB setup, there is no clue given that would tell Gradle which variant to select; so it fails (with an admittedly bad/misleading error message). In my changed testB setup I provide the clue: I tell Gradle that we want the variant that has a specific attribute with the value my-zipped-lib . Since I’ve added the same attribute on the published configuration of testA , Gradle is now able to select the right variant (because there is only one that has the required attribute). The file extension on the dependency is still relevant in a second step: once Gradle has selected the component variant, it still needs to select the right artifact – but only then.
Читайте также:  Zip file for java

Note that we’re actually working on the rim of what is supported with composite builds today. See also Gradle issue #2529 which states that “projects that publish non-jar artifacts” were not terribly well supported. When I first saw your question I honestly thought we’d be out of luck here … but it seems there’s a way to again step a little closer to the rim 😉

In the comments, the question came up why adding multiple custom artifacts works while applying the java plugin breaks the build. As I’ve tried to explain above, it’s not a question of multiple artifacts but of multiple component variants. IIUIC, these variants originate from different attributes on configurations. When you don’t add such attributes, then you will not have different components variants. However, the Java plugin does add such attributes which consequently leads to different component variants in your project(/component). If you’re interested, you can see the different attributes by adding something like the following to your build.gradle :

configurations.each < conf ->println "Attributes of $conf:" conf.attributes.keySet().each < attr ->println "t$attr -> $" > > 

Now where and when to add which attributes? That depends on your setup. I wouldn’t blindly add attributes to all configurations in the hope that that’ll solve things magically. While it might work (depending on your setup), it’d certainly not be clean. If your project setup is as complex and/or special as your question suggests, then it’ll probably make sense to think more deeply about which configurations you need and what attributes they should carry. The first sections of the Gradle documentation page that I have linked above are probably a good starting point if you are not intricately familiar with these nuances of configurations in Gradle, yet. And yes, I agree that such logic would best live in a Gradle plugin.

Источник

Could not import third-party class in gradle file other than build.gradle

I want to import a third-party class org.ajoberstar.grgit.Grgit in a gradle file version.gradle . However it is giving me the error that it is unable to resolve class org.ajoberstar.grgit.Grgit(I am using apply from: «version.gradle» in build.gradle). But if I import it in build.gradle , it works fine. Here is what I am doing: build.gradle:

plugins < id "org.ajoberstar.grgit" version "1.5.0" >apply from: "version.gradle" // Version of jar file. version = 1.0 jar < destinationDir = file(JAR_DIR) from < (configurations.runtime).collect < it.isDirectory() ? it : zipTree(it) >> manifest < attributes 'Main-Class': 'com.awesomeness.Main' >> jar.dependsOn versionTxt // Artifact dependecies. dependencies < compile files("$TOOLS/lib/grgit-1.5.0.jar") compile files("$TOOLS/lib/groovy-all-2.4.7.jar") compile files("$TOOLS/lib/org.eclipse.jgit-4.2.0.201601211800-r.jar") >
import org.ajoberstar.grgit.Grgit // import java.text.DateFormat task versionTxt() < doLast < Grgit grgit = Grgit.open() File file = new File("$buildDir/version.txt") file.write("") file.append("Version: $version") file.append("Branch: "+grgit.branch.current.trackingBranch.name) file.append("Build-Type: ") file.append("Build-Date: " + DateFormat.getDateInstance(DateFormat.SHORT).format(new Date((long)grgit.head().time*1000l))) file.append("Commit-Id: "+ grgit.head().abbreviatedId) >> 

But I could not resolve this. Any help?

Источник

Оцените статью