Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removes the jpa facet when it's not used. #47

Merged
merged 1 commit into from
May 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ Please provide a unit test(s) for any patch and if one doesn't exist it may not
* 2/2 spacing
* 120 characters for lines and comments

##Flags

* Jpa facet will turn on if the artifact `<groupId>javax.jdo</groupId> <artifactId>jdo-api</artifactId>` exists.

#GPE V1

##Links
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ org.eclipse.jdt.core.formatter.keep_else_statement_on_same_line=false
org.eclipse.jdt.core.formatter.keep_empty_array_initializer_on_one_line=false
org.eclipse.jdt.core.formatter.keep_imple_if_on_one_line=true
org.eclipse.jdt.core.formatter.keep_then_statement_on_same_line=false
org.eclipse.jdt.core.formatter.lineSplit=100
org.eclipse.jdt.core.formatter.lineSplit=120
org.eclipse.jdt.core.formatter.never_indent_block_comments_on_first_column=false
org.eclipse.jdt.core.formatter.never_indent_line_comments_on_first_column=false
org.eclipse.jdt.core.formatter.number_of_blank_lines_at_beginning_of_method_body=0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ comment_new_line_for_parameter=false
comment_separate_root_tags=true
eclipse.preferences.version=1
editor_save_participant_org.eclipse.jdt.ui.postsavelistener.cleanup=true
formatter_profile=_GPE
formatter_profile=_GWT-Plugin
formatter_settings_version=12
org.eclipse.jdt.ui.exception.name=e
org.eclipse.jdt.ui.gettersetter.use.is=true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@ public class Constants {
// Components of appengine-maven-plugin Maven coordinates:
public static final String APPENGINE_GROUP_ID = "com.google.appengine";
public static final String APPENGINE_MAVEN_PLUGIN_ARTIFACT_ID = "appengine-maven-plugin";

// Components of datanucleus-maven-plugin coordinates:
public static final String DATANUCLEUS_GROUP_ID = "org.datanucleus";
public static final String DATANUCLEUS_MAVEN_PLUGIN_ARTIFACT_ID = "datanucleus-maven-plugin";

// Facet IDs:
public static final String GAE_WAR_FACET_ID = IGaeFacetConstants.GAE_FACET_ID;
public static final String GAE_EAR_FACET_ID = IGaeFacetConstants.GAE_EAR_FACET_ID;
public static final String JPA_FACET_ID = "jpt.jpa";

// JPA artifact <groupId>javax.jdo</groupId> <artifactId>jdo-api</artifactId>
public static final String JPA_ARTIFACT = "jdo-api";

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
*******************************************************************************/
package com.google.appengine.eclipse.wtp.maven;

import java.util.List;

import org.apache.maven.model.Model;
import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.MojoExecution;
Expand All @@ -30,10 +28,13 @@
import org.eclipse.wst.common.project.facet.core.IFacetedProject;
import org.eclipse.wst.common.project.facet.core.ProjectFacetsManager;

import java.util.List;

/**
* A {@link WTPProjectConfigurator} that adds GAE and JPA facets to GAE projects.
*/
@SuppressWarnings("restriction") // WTPProjectConfigurator
@SuppressWarnings("restriction")
// WTPProjectConfigurator
public class GaeProjectConfigurator extends WTPProjectConfigurator {

@Override
Expand All @@ -44,18 +45,19 @@ public void configure(ProjectConfigurationRequest request, IProgressMonitor moni
IProject eclipseProject = request.getProject();
IFacetedProject facetedProject = ProjectFacetsManager.create(eclipseProject);
new GaeFacetManager().addGaeFacet(pom, facetedProject, monitor);
new JpaFacetManager().configureJpaFacet(facetedProject, monitor);

// Only turn on the facet if the jpa artifact exists
new JpaFacetManager().configureJpaFacet(facetedProject, monitor, isJpaProject(pom));
}
}

@Override
public AbstractBuildParticipant getBuildParticipant(
IMavenProjectFacade projectFacade, MojoExecution execution,
IPluginExecutionMetadata executionMetadata) {
public AbstractBuildParticipant getBuildParticipant(IMavenProjectFacade projectFacade,
MojoExecution execution, IPluginExecutionMetadata executionMetadata) {
AppEngineMavenPlugin.logInfo("GaeProjectConfigurator.getBuildParticipant invoked");
return super.getBuildParticipant(projectFacade, execution, executionMetadata);
}

private static boolean isGaeProject(Model pom) {
List<Plugin> plugins = pom.getBuild().getPlugins();
for (Plugin plugin : plugins) {
Expand All @@ -67,4 +69,17 @@ private static boolean isGaeProject(Model pom) {
return false;
}

/**
* If there is a JPA artifact, turn on JPA or turn it off
*/
private boolean isJpaProject(Model pom) {
List<Plugin> plugins = pom.getBuild().getPlugins();
for (Plugin plugin : plugins) {
if (Constants.JPA_ARTIFACT.equals(plugin.getArtifactId())) {
return true;
}
}
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,52 +31,71 @@
*/
public class JpaFacetManager {

private static final String APP_ENGINE_JPA_PLATFORM_ID =
"com.google.appengine.eclipse.wtp.jpa.GaePlatform";
// Defined in the org.eclipse.jpt.jpa.core.jpaPlatforms extension point in
// com.google.appengine.eclipse.wtp.jpa.e43/plugin.xml
private static final String APP_ENGINE_JPA_PLATFORM_ID = "com.google.appengine.eclipse.wtp.jpa.GaePlatform";

// Defined in the org.eclipse.jpt.jpa.core.jpaPlatforms extension point in
// com.google.appengine.eclipse.wtp.jpa.e43/plugin.xml

/**
* Configures the JPA facet of a specified faceted project to use the implementation of JPA
* bundled with App Engine, by ensuring that the latest JPA project facet is installed, setting
* the JPA platform in project preferences to App Engine, and adding the builder for the
* App Engine Datanucleus enhancer to the project.
* the JPA platform in project preferences to App Engine, and adding the builder for the App
* Engine Datanucleus enhancer to the project.
*
* @param facetedProject the specified faceted project
* @param monitor a progress monitor for the operation
* @param enableFacet
* @throws CoreException if there is an error adding the builder
*/
public void configureJpaFacet(
IFacetedProject facetedProject, IProgressMonitor monitor) throws CoreException {
IProjectFacetVersion installedVersion =
ensureLatestJpaFacetVersionInstalled(facetedProject, monitor);
if (installedVersion != null) {
JpaFacetHelper.setJpaPlatformId(facetedProject.getProject(), APP_ENGINE_JPA_PLATFORM_ID);
BuilderUtilities.addBuilderToProject(
facetedProject.getProject(), AppEnginePlugin.PLUGIN_ID + ".enhancerbuilder");
public void configureJpaFacet(IFacetedProject facetedProject, IProgressMonitor monitor, boolean enableFacet)
throws CoreException {
if (enableFacet) {
IProjectFacetVersion installedVersion = ensureLatestJpaFacetVersionInstalled(facetedProject, monitor);
if (installedVersion != null) {
JpaFacetHelper.setJpaPlatformId(facetedProject.getProject(), APP_ENGINE_JPA_PLATFORM_ID);
BuilderUtilities.addBuilderToProject(facetedProject.getProject(), AppEnginePlugin.PLUGIN_ID
+ ".enhancerbuilder");
}
} else {
removeProjectFacet(facetedProject, monitor);
BuilderUtilities.removeBuilderFromProject(facetedProject.getProject(), AppEnginePlugin.PLUGIN_ID
+ ".enhancerbuilder");
}
}

/**
* Remove jpaFacet
*
* @param facetedProject
* @param monitor
* @throws CoreException
*/
private void removeProjectFacet(IFacetedProject facetedProject, IProgressMonitor monitor) throws CoreException {
IProjectFacet jpaFacet = ProjectFacetsManager.getProjectFacet(Constants.JPA_FACET_ID);
IFacetedProjectWorkingCopy workingCopy = facetedProject.createWorkingCopy();
workingCopy.removeProjectFacet(jpaFacet);
workingCopy.commitChanges(monitor);
}

/**
* Ensure that the latest available JPA facet version is installed in a specified faceted project.
* If no JPA facet is installed, the latest JPA facet version is installed. If an earlier JPA
* facet version is installed, it is upgraded to the latest version.
*
* @param facetedProject the specified faceted project
* @param monitor a progress monitor for the operation
* @return
* the installed version, or {@code null} in the event (which is supposed to be impossible)
* that no JPA facet version can be found
* @return the installed version, or {@code null} in the event (which is supposed to be
* impossible) that no JPA facet version can be found
*/
private IProjectFacetVersion ensureLatestJpaFacetVersionInstalled(
IFacetedProject facetedProject, IProgressMonitor monitor) {
private IProjectFacetVersion ensureLatestJpaFacetVersionInstalled(IFacetedProject facetedProject,
IProgressMonitor monitor) {
IProjectFacet jpaFacet = ProjectFacetsManager.getProjectFacet(Constants.JPA_FACET_ID);
IFacetedProjectWorkingCopy workingCopy = facetedProject.createWorkingCopy();

IProjectFacetVersion latestVersion = workingCopy.getHighestAvailableVersion(jpaFacet);
if (latestVersion == null) {
// This shouldn't happen.
AppEngineMavenPlugin.logError(
"Unable to find an available version of the JPA facet", null);
AppEngineMavenPlugin.logError("Unable to find an available version of the JPA facet", null);
return null;
}
// Although it's not documented, the source code of the class implementing IFacetedProject
Expand Down