-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
130 changes: 130 additions & 0 deletions
130
...l-projects/src/main/java/org/jboss/forge/maven/projects/facets/MavenWebResourceFacet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/* | ||
* Copyright 2013 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Eclipse Public License version 1.0, available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
package org.jboss.forge.maven.projects.facets; | ||
|
||
import java.io.File; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import javax.enterprise.context.Dependent; | ||
|
||
import org.jboss.forge.dependencies.Coordinate; | ||
import org.jboss.forge.dependencies.builder.CoordinateBuilder; | ||
import org.jboss.forge.facets.AbstractFacet; | ||
import org.jboss.forge.maven.plugins.ConfigurationElementBuilder; | ||
import org.jboss.forge.maven.plugins.MavenPlugin; | ||
import org.jboss.forge.maven.plugins.MavenPluginBuilder; | ||
import org.jboss.forge.maven.projects.MavenFacet; | ||
import org.jboss.forge.maven.projects.MavenPluginFacet; | ||
import org.jboss.forge.projects.Project; | ||
import org.jboss.forge.projects.facets.PackagingFacet; | ||
import org.jboss.forge.projects.facets.WebResourceFacet; | ||
import org.jboss.forge.resource.DirectoryResource; | ||
import org.jboss.forge.resource.FileResource; | ||
|
||
@Dependent | ||
public class MavenWebResourceFacet extends AbstractFacet<Project> implements WebResourceFacet | ||
{ | ||
|
||
@Override | ||
public DirectoryResource getWebRootDirectory() | ||
{ | ||
return getOrigin().getProjectRoot() | ||
.getChildDirectory("src" + File.separator + "main" + File.separator + "webapp"); | ||
} | ||
|
||
@Override | ||
public List<DirectoryResource> getWebRootDirectories() | ||
{ | ||
List<DirectoryResource> result = new ArrayList<DirectoryResource>(); | ||
result.add(getWebRootDirectory()); | ||
return result; | ||
} | ||
|
||
@Override | ||
public boolean isInstalled() | ||
{ | ||
Project project = getOrigin(); | ||
MavenFacet mavenFacet = project.getFacet(MavenFacet.class); | ||
String packagingType = project.getFacet(PackagingFacet.class).getPackagingType(); | ||
|
||
return mavenFacet.isInstalled() | ||
&& packagingType.equals("war"); | ||
} | ||
|
||
@Override | ||
public boolean install() | ||
{ | ||
if (!this.isInstalled()) | ||
{ | ||
for (DirectoryResource folder : this.getWebRootDirectories()) | ||
{ | ||
folder.mkdirs(); | ||
} | ||
|
||
MavenPluginFacet plugins = getOrigin().getFacet(MavenPluginFacet.class); | ||
Coordinate mvnWarPluginDep = CoordinateBuilder.create().setGroupId("org.apache.maven.plugins") | ||
.setArtifactId("maven-war-plugin") | ||
.setVersion("2.3"); | ||
|
||
MavenPlugin plugin; | ||
if (!plugins.hasPlugin(mvnWarPluginDep)) | ||
{ | ||
plugin = MavenPluginBuilder.create().setCoordinate(mvnWarPluginDep); | ||
plugins.addPlugin(plugin); | ||
} | ||
else | ||
{ | ||
plugin = plugins.getPlugin(mvnWarPluginDep); | ||
} | ||
|
||
if (plugin.getConfig() == null) | ||
{ | ||
|
||
} | ||
|
||
if (!plugin.getConfig().hasConfigurationElement("failOnMissingWebXml")) | ||
{ | ||
plugin.getConfig().addConfigurationElement( | ||
ConfigurationElementBuilder.create().setName("failOnMissingWebXml").setText("false")); | ||
} | ||
else | ||
{ | ||
ConfigurationElementBuilder configElement = ConfigurationElementBuilder.createFromExisting(plugin | ||
.getConfig().getConfigurationElement("failOnMissingWebXml")); | ||
plugin.getConfig().removeConfigurationElement("failOnMissingWebXml"); | ||
plugin.getConfig().addConfigurationElement(configElement); | ||
} | ||
|
||
plugins.removePlugin(mvnWarPluginDep); | ||
plugins.addPlugin(plugin); | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public FileResource<?> getWebResource(final String relativePath) | ||
{ | ||
return (FileResource<?>) getWebRootDirectory().getChild(relativePath); | ||
} | ||
|
||
@Override | ||
public FileResource<?> createWebResource(final char[] data, final String relativePath) | ||
{ | ||
FileResource<?> file = (FileResource<?>) getWebRootDirectory().getChild(relativePath); | ||
file.setContents(data); | ||
return file; | ||
} | ||
|
||
@Override | ||
public FileResource<?> createWebResource(final String data, final String relativePath) | ||
{ | ||
return createWebResource(data.toCharArray(), relativePath); | ||
} | ||
|
||
} |
58 changes: 58 additions & 0 deletions
58
projects/api/src/main/java/org/jboss/forge/projects/facets/WebResourceFacet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright 2013 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Eclipse Public License version 1.0, available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
package org.jboss.forge.projects.facets; | ||
|
||
import java.util.List; | ||
|
||
import org.jboss.forge.facets.Facet; | ||
import org.jboss.forge.projects.ProjectFacet; | ||
import org.jboss.forge.resource.DirectoryResource; | ||
import org.jboss.forge.resource.FileResource; | ||
|
||
/** | ||
* A {@link Facet} containing APIs to interact with Java Web Projects | ||
* | ||
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a> | ||
*/ | ||
public interface WebResourceFacet extends ProjectFacet | ||
{ | ||
/** | ||
* Get the default Web Root directory (this is the {@link DirectoryResource} containing resources to be deployed to | ||
* the web-root URL when the application is published. (E.g. In a maven project, files in the | ||
* <code>/project/src/main/webapp</code> directory are typically published to the root URL: | ||
* <code>http://localhost:8080/project/</code> root directory. In an eclipse project, this folder is typically | ||
* located by default at: <code>/project/WebContent/</code>.) | ||
*/ | ||
DirectoryResource getWebRootDirectory(); | ||
|
||
/** | ||
* Get a list containing all possible Web Root {@link DirectoryResource}s for the current project. | ||
*/ | ||
List<DirectoryResource> getWebRootDirectories(); | ||
|
||
/** | ||
* At the given path/filename relative to the project Web Root directory: {@link #getWebRootDirectory()} - create a | ||
* {@link FileResource} containing the given bytes. | ||
* | ||
* @return a handle to the {@link FileResource} that was created. | ||
*/ | ||
FileResource<?> createWebResource(char[] bytes, String relativeFilename); | ||
|
||
/** | ||
* At the given path/filename relative to the project Web Root directory: {@link #getWebRootDirectory()} - create a | ||
* {@link FileResource} containing the given String. | ||
* | ||
* @return a handle to the {@link FileResource} that was created. | ||
*/ | ||
FileResource<?> createWebResource(String render, String relativeFilename); | ||
|
||
/** | ||
* Get the given {@link FileResource} relative to the project Web Root directory: {@link #getWebRootDirectory()} | ||
*/ | ||
FileResource<?> getWebResource(String relativePath); | ||
} |