Search in sources :

Example 1 with UnknownConfigurationException

use of org.gradle.api.artifacts.UnknownConfigurationException in project tomee by apache.

the class TomEEEmbeddedPlugin method apply.

@Override
public void apply(final Project project) {
    final List<String> extensions = asList(TomEEEmbeddedExtension.NAME, TomEEEmbeddedExtension.ALIAS);
    for (final String name : extensions) {
        project.getExtensions().create(name, TomEEEmbeddedExtension.class);
    }
    project.afterEvaluate(new Action<Project>() {

        @Override
        public void execute(final Project actionProject) {
            for (final String name : extensions) {
                final TomEEEmbeddedExtension extension = TomEEEmbeddedExtension.class.cast(actionProject.getExtensions().findByName(name));
                if (extension == null) {
                    return;
                }
                if (extension.isSkipDefaultRepository() != null && !extension.isSkipDefaultRepository()) {
                    actionProject.getRepositories().mavenCentral();
                    return;
                }
            }
            actionProject.getRepositories().mavenCentral();
        }
    });
    String configName = TomEEEmbeddedExtension.ALIAS;
    try {
        project.getConfigurations().getByName(configName);
    } catch (final UnknownConfigurationException uce) {
        configName = TomEEEmbeddedExtension.NAME;
    }
    final Configuration configuration = project.getConfigurations().maybeCreate(configName);
    configuration.getIncoming().beforeResolve(new Action<ResolvableDependencies>() {

        @Override
        public void execute(final ResolvableDependencies resolvableDependencies) {
            String tomeeVersion = null;
            for (final String name : extensions) {
                final TomEEEmbeddedExtension extension = TomEEEmbeddedExtension.class.cast(project.getExtensions().findByName(name));
                if (extension == null) {
                    return;
                }
                tomeeVersion = extension.getTomeeVersion();
                if (tomeeVersion != null) {
                    break;
                }
            }
            if (tomeeVersion == null) {
                try {
                    try (final InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("META-INF/maven/org.apache.tomee.gradle/tomee-embedded/pom.properties")) {
                        final Properties p = new Properties();
                        p.load(is);
                        tomeeVersion = p.getProperty("version");
                    }
                } catch (final IOException e) {
                    // we should never be there
                    tomeeVersion = "7.0.2";
                }
            }
            final DependencyHandler dependencyHandler = project.getDependencies();
            final DependencySet dependencies = configuration.getDependencies();
            dependencies.add(dependencyHandler.create("org.apache.tomee:tomee-embedded:" + tomeeVersion));
        }
    });
    project.task(new HashMap<String, Object>() {

        {
            put("type", TomEEEmbeddedTask.class);
            put("group", "Embedded Application Server");
            put("description", "Start an embedded Apache TomEE server deploying application classpath");
        }
    }, TomEEEmbeddedExtension.NAME);
}
Also used : UnknownConfigurationException(org.gradle.api.artifacts.UnknownConfigurationException) Configuration(org.gradle.api.artifacts.Configuration) InputStream(java.io.InputStream) DependencySet(org.gradle.api.artifacts.DependencySet) IOException(java.io.IOException) Properties(java.util.Properties) ResolvableDependencies(org.gradle.api.artifacts.ResolvableDependencies) Project(org.gradle.api.Project) DependencyHandler(org.gradle.api.artifacts.dsl.DependencyHandler)

Example 2 with UnknownConfigurationException

use of org.gradle.api.artifacts.UnknownConfigurationException in project tomee by apache.

the class TomEEEmbeddedTask method fixConfig.

private void fixConfig() {
    final Project project = getProject();
    // defaults
    if (classpath == null) {
        try {
            classpath.add(project.getConfigurations().getByName(TomEEEmbeddedExtension.ALIAS).fileCollection());
        } catch (final UnknownConfigurationException uce) {
            classpath = project.getConfigurations().getByName(TomEEEmbeddedExtension.NAME);
        }
    }
    if (docBase == null) {
        docBase = new File(project.getProjectDir(), "src/main/webapp");
    }
    if (workDir == null) {
        workDir = new File(project.getBuildDir(), "tomee-embedded/work");
    }
    if (dir == null) {
        dir = new File(project.getBuildDir(), "tomee-embedded/run").getAbsolutePath();
    }
    if (modules == null || modules.isEmpty()) {
        final File main = new File(project.getBuildDir(), "classes/main");
        if (main.isDirectory()) {
            modules = new ArrayList<>(singletonList(main));
        }
    }
    // extension override
    for (final String name : asList(TomEEEmbeddedExtension.NAME, TomEEEmbeddedExtension.ALIAS)) {
        final TomEEEmbeddedExtension extension = TomEEEmbeddedExtension.class.cast(project.getExtensions().findByName(name));
        if (extension != null) {
            for (final Field f : TomEEEmbeddedTask.class.getDeclaredFields()) {
                if (f.isAnnotationPresent(Input.class)) {
                    try {
                        final Field extField = TomEEEmbeddedExtension.class.getDeclaredField(f.getName());
                        if (!extField.isAccessible()) {
                            extField.setAccessible(true);
                        }
                        final Object val = extField.get(extension);
                        if (val != null) {
                            if (!f.isAccessible()) {
                                f.setAccessible(true);
                            }
                            f.set(this, val);
                        }
                    } catch (final IllegalAccessException | NoSuchFieldException e) {
                        getLogger().warn("No field " + f.getName() + " in " + extension, e);
                    }
                }
            }
        }
    }
}
Also used : Project(org.gradle.api.Project) UnknownConfigurationException(org.gradle.api.artifacts.UnknownConfigurationException) Field(java.lang.reflect.Field) File(java.io.File)

Aggregations

Project (org.gradle.api.Project)2 UnknownConfigurationException (org.gradle.api.artifacts.UnknownConfigurationException)2 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Field (java.lang.reflect.Field)1 Properties (java.util.Properties)1 Configuration (org.gradle.api.artifacts.Configuration)1 DependencySet (org.gradle.api.artifacts.DependencySet)1 ResolvableDependencies (org.gradle.api.artifacts.ResolvableDependencies)1 DependencyHandler (org.gradle.api.artifacts.dsl.DependencyHandler)1