问题
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.
回答1:
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 ????????
回答2:
I tend to agree, Gradle needs to work like native build, Ant. Even in Netbeans, I can create A Gradle project, runs perfect. But don't dare update anything involving Gradle or Groovy, you will have a ruined project. Ant has worked for me for years, I never had to read one word of the documentation, just does it's job right.
来源:https://stackoverflow.com/questions/48828869/how-to-convert-java-gradle-project-to-dynamic-web-project