Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Java Gradle project to Dynamic Web Project?

I am following this article

https://www.linkedin.com/pulse/building-jax-rs-project-eclipse-gradle-neeraj-malhotra

On how to build a JAX-RS project in Eclipse with Gradle

I am using

Eclipse Java EE IDE for Web Developers.

Version: Oxygen.2 Release (4.7.2)
Build id: 20171218-0600

My target runtime is

WildFly Full 11.0.0.Final (WildFly Core 3.0.8.Final)

my gradle build file

apply plugin: 'war'
apply plugin: 'eclipse-wtp'

sourceCompatibility = 1.8

repositories {
 mavenCentral()
}


dependencies {
 compile "javax.ws.rs:javax.ws.rs-api:2.0.1"
 providedCompile "javax.servlet:javax.servlet-api:3.1.0" 
}

I can select the required Facets and set target runtimes etc fine.

The Facets I select are

cdi 2.0
dynamic web module 3.1
java 1.8
jax-rs 2.1

Once I have click on Apply & Close the project has no build issues reported.

What I don't like at this point is that the project has two WEB-INF files one is at the project level, the other is within the WebContent folder where I would expect it.

There is also a web.xml file even though I specifically didn't select to have a web.xml generated.

The real issues start when I refresh my gradle build by right clicking on the gradle.build file and selecting gradle > refresh gradle project.

When the refresh is complete the following facets have been deselected

cdi 2.0
jax-rs 2.1

and my Dynamic Web Module has been downgraded to 2.4 from 3.1

In addition to this I have the following two MARKERS

CDI Core Validator cannot run on project gradle_jax_rs because Validation Builder precedes CDI (Contexts and Dependency Injection) Builder.

EL Validator cannot run on project gradle_jax_rs because Validation Builder precedes JBoss Knowledge Base Builder.

How can I configure my Dynamic Web Project so that it keeps my desired Facets at the level?

What is my gradle.build missing?

UPDATE

So I've tracked down the culprit in Gradle

org.gradle.plugins.ide.eclipse.EclipseWtpPlugin:-

project.getPlugins().withType(WarPlugin.class, new Action<WarPlugin>() {
                    @Override
                    public void execute(WarPlugin warPlugin) {
                        ((IConventionAware) task.getFacet()).getConventionMapping().map("facets", new Callable<List<Facet>>() {
                            @Override
                            public List<Facet> call() throws Exception {
                                return Lists.newArrayList(
                                    new Facet(Facet.FacetType.fixed, "jst.java", null),
                                    new Facet(Facet.FacetType.fixed, "jst.web", null),
                                    new Facet(Facet.FacetType.installed, "jst.web", "2.4"),
                                    new Facet(Facet.FacetType.installed, "jst.java", toJavaFacetVersion(project.getConvention().getPlugin(JavaPluginConvention.class).getSourceCompatibility()))
                                );
                            }
                        });
                    }

                });

This results in duplicate jst.web entries in org.eclipse.wst.common.project.facet.core.xml

<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
    <fixed facet="jst.java"/>
    <fixed facet="jst.web"/>
    <installed facet="jst.web" version="2.4"/>
    <installed facet="jst.java" version="1.8"/>
    <installed facet="jst.web" version="3.1"/>
</faceted-project>

Why cant Gradle work correctly. Whats hard about building a Dynamic Web Project with the selected facets?

I have upgraded buildship to... http://download.eclipse.org/buildship/updates/e47/releases/2.x/2.2.1.v20180125-1441

even with this added to my gradle.build it still cannot build the required dynamic web project correctly

eclipse {
            wtp {
                facet {
                    facet name: 'java', version: '1.8'
                    facet name: 'jst.web', version: '3.1'
                    facet name: 'wst.jsdt.web', version: '1.0'
                }
            } 
        } 

Found that this clunky hack fixes the duplicate jst.web versions

apply plugin: 'war'
apply plugin: 'eclipse-wtp'

eclipse.wtp.facet {
    file {
            facet name: 'jst.web', version: '3.1'
            def oldJstWebFacet = facets.findAll {
                it.name == 'jst.web' && it.version == '2.4'
            }
            facets.removeAll(oldJstWebFacet)
            facet name: 'java', version: '1.8'
            facet name: 'jst.cdi', version: '2.0'
            facet name: 'jst.jaxrs', version: '2.1'
            facet name: 'wst.jsdt.web', version: '1.0'              
        }
}

repositories {
 mavenCentral()
}

dependencies {
 compile "javax.ws.rs:javax.ws.rs-api:2.0.1"
 providedCompile "javax.servlet:javax.servlet-api:3.1.0" 
}

however now I have the following two eclipse error markers

CDI Core Validator cannot run on project Attempt0002 because Validation Builder precedes CDI (Contexts and Dependency Injection) Builder.

EL Validator cannot run on project Attempt0002 because Validation Builder precedes JBoss Knowledge Base Builder.

I can resolve this issue by amending the builder order within eclipse, however as soon as I build my project with Gradle the issue reappears.

like image 988
Hector Avatar asked Feb 16 '18 14:02

Hector


People also ask

How do I fix the dynamic Web project missing in Eclipse?

Step 1: Click Help -> Install New Software. Step 2: In Work with add http://download.eclipse.org/releases/oxygen. If you are using any other Eclipse product like Neon then replace oxygen with neon. Step 3: Now expand checkbox with name Web, XML, Java EE and OSGi Enterprise Development.

What is dynamic Web project?

Dynamic web projects have more advanced components, such as Java, PHP, ASP, etc. They have a lot of focus on server-side development. Dynamic web projects offer many advantages over static web projects as they can provide more information and functionalities.

How will u convert an already existing Java project into a Gradle project?

To convert a Java project to use Gradle, select Gradle Add Gradle Nature from the context menu of the project. Run the 'gradle init' task to create the initial Gradle files, in case you do not have them yet.


1 Answers

The solution I discovered is as follows:-

HACK the gradle build file as follows:-

apply plugin: 'war'
apply plugin: 'eclipse-wtp'

eclipse {
    project {
            buildCommands.clear();
            buildCommand 'org.eclipse.jdt.core.javabuilder'
            buildCommand 'org.eclipse.wst.common.project.facet.core.builder'
            buildCommand 'org.eclipse.buildship.core.gradleprojectbuilder'
            buildCommand 'org.jboss.tools.jst.web.kb.kbbuilder'
            buildCommand 'org.jboss.tools.cdi.core.cdibuilder'
            buildCommand 'org.eclipse.wst.validation.validationbuilder'
   }
  wtp {
    facet {
      file {
            facet name: 'jst.web', version: '3.1'
            def oldJstWebFacet = facets.findAll {
                it.name == 'jst.web' && it.version == '2.4'
            }
            facets.removeAll(oldJstWebFacet)
            facet name: 'java', version: '1.8'
            facet name: 'jst.cdi', version: '2.0'
            facet name: 'jst.jaxrs', version: '2.1'
            facet name: 'wst.jsdt.web', version: '1.0'          
      }
    }
  }
}

repositories {
 mavenCentral()
}

dependencies {
 compile "javax.ws.rs:javax.ws.rs-api:2.0.1"
 providedCompile "javax.servlet:javax.servlet-api:3.1.0" 
}

"Accelerate developer productivity" really ????????

like image 176
Hector Avatar answered Nov 01 '22 09:11

Hector