Skip to content

Commit

Permalink
Make IvyArtifact lazy
Browse files Browse the repository at this point in the history
  • Loading branch information
oehme committed Apr 24, 2018
1 parent bd9568d commit e076a78
Show file tree
Hide file tree
Showing 14 changed files with 541 additions and 183 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,23 @@ The following types/formats are supported:
- Anything that can be converted to a file, as per Project.file()""")
}


def "artifact coordinates are evaluated lazily"() {
given:
createBuildScripts("""
publications.create("ivyCustom", IvyPublication) {
artifact customJar
}
""", "version = 2.0")
when:
succeeds 'publish'

then:
def module = ivyRepo.module("org.gradle.test", "ivyPublish", "2.0")
module.assertPublished()
module.assertArtifactsPublished("ivy-2.0.xml", "ivyPublish-2.0.jar")
}

private createBuildScripts(def publications, def append = "") {
file("customFile.txt") << "some content"
settingsFile << "rootProject.name = 'ivyPublish'"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* Copyright 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.gradle.api.publish.ivy.internal.artifact;

import com.google.common.base.Strings;
import org.gradle.api.internal.tasks.AbstractTaskDependency;
import org.gradle.api.internal.tasks.DefaultTaskDependency;
import org.gradle.api.internal.tasks.TaskDependencyInternal;
import org.gradle.api.internal.tasks.TaskDependencyResolveContext;
import org.gradle.api.publish.ivy.IvyArtifact;
import org.gradle.api.tasks.TaskDependency;

import javax.annotation.Nullable;

public abstract class AbstractIvyArtifact implements IvyArtifact {
private final TaskDependency allBuildDependencies;
private final DefaultTaskDependency additionalBuildDependencies;

private String name;
private String type;
private String extension;
private String classifier;
private String conf;

protected AbstractIvyArtifact() {
this.additionalBuildDependencies = new DefaultTaskDependency();
this.allBuildDependencies = new CompositeTaskDependency();
}

@Override
public String getName() {
return name != null ? name : getDefaultName();
}

protected abstract String getDefaultName();

@Override
public void setName(String name) {
this.name = Strings.nullToEmpty(name);
}

@Override
public String getType() {
return type != null ? type : getDefaultType();
}

protected abstract String getDefaultType();

@Override
public void setType(String type) {
this.type = Strings.nullToEmpty(type);
}

@Override
public String getExtension() {
return extension != null ? extension : getDefaultExtension();
}

protected abstract String getDefaultExtension();

@Override
public void setExtension(String extension) {
this.extension = Strings.nullToEmpty(extension);
}

@Nullable
@Override
public String getClassifier() {
return Strings.emptyToNull(classifier != null ? classifier : getDefaultClassifier());
}

protected abstract String getDefaultClassifier();

@Override
public void setClassifier(@Nullable String classifier) {
this.classifier = Strings.nullToEmpty(classifier);
}

@Nullable
@Override
public String getConf() {
return Strings.emptyToNull(conf != null ? conf : getDefaultConf());
}

protected abstract String getDefaultConf();

@Override
public void setConf(@Nullable String conf) {
this.conf = Strings.nullToEmpty(conf);
}

@Override
public void builtBy(Object... tasks) {
additionalBuildDependencies.add(tasks);
}

@Override
public TaskDependency getBuildDependencies() {
return allBuildDependencies;
}

protected abstract TaskDependencyInternal getDefaultBuildDependencies();

@Override
public String toString() {
return String.format("%s %s:%s:%s:%s", getClass().getSimpleName(), getName(), getType(), getExtension(), getClassifier());
}

private class CompositeTaskDependency extends AbstractTaskDependency {
@Override
public void visitDependencies(TaskDependencyResolveContext context) {
getDefaultBuildDependencies().visitDependencies(context);
additionalBuildDependencies.visitDependencies(context);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.gradle.api.publish.ivy.internal.artifact;

import com.google.common.collect.ImmutableSet;
import org.gradle.api.internal.tasks.DefaultTaskDependency;
import org.gradle.api.internal.tasks.TaskDependencyInternal;
import org.gradle.api.publish.ivy.internal.publisher.IvyPublicationIdentity;
import org.gradle.api.tasks.bundling.AbstractArchiveTask;

import java.io.File;

public class ArchiveTaskBasedIvyArtifact extends AbstractIvyArtifact {
private final AbstractArchiveTask archiveTask;
private final IvyPublicationIdentity identity;
private final TaskDependencyInternal buildDependencies;

public ArchiveTaskBasedIvyArtifact(AbstractArchiveTask archiveTask, IvyPublicationIdentity identity) {
this.archiveTask = archiveTask;
this.identity = identity;
this.buildDependencies = new DefaultTaskDependency(null, ImmutableSet.<Object>of(archiveTask));
}

@Override
protected String getDefaultName() {
return identity.getModule();
}

@Override
protected String getDefaultType() {
return archiveTask.getExtension();
}

@Override
protected String getDefaultExtension() {
return archiveTask.getExtension();
}

@Override
protected String getDefaultClassifier() {
return archiveTask.getClassifier();
}

@Override
protected String getDefaultConf() {
return null;
}

@Override
protected TaskDependencyInternal getDefaultBuildDependencies() {
return buildDependencies;
}

@Override
public File getFile() {
return archiveTask.getArchivePath();
}
}

This file was deleted.

0 comments on commit e076a78

Please sign in to comment.