diff --git a/maven.groovy/src/org/netbeans/modules/maven/groovy/GroovyCompileOnSaveClassCleaner.java b/maven.groovy/src/org/netbeans/modules/maven/groovy/GroovyCompileOnSaveClassCleaner.java new file mode 100644 --- /dev/null +++ b/maven.groovy/src/org/netbeans/modules/maven/groovy/GroovyCompileOnSaveClassCleaner.java @@ -0,0 +1,137 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2012 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2012 Sun Microsystems, Inc. + */ + +package org.netbeans.modules.maven.groovy; + +import java.io.IOException; +import org.netbeans.api.project.Project; +import org.netbeans.api.project.ProjectUtils; +import org.netbeans.api.project.SourceGroup; +import org.netbeans.modules.groovy.support.api.GroovySources; +import org.netbeans.modules.maven.spi.cos.CompileOnSaveClassCleaner; +import org.netbeans.spi.project.ProjectServiceProvider; +import org.openide.filesystems.FileObject; +import org.openide.util.Exceptions; + +/** + * + * @author Martin Janicek + */ +@ProjectServiceProvider( + projectType = "org-netbeans-modules-maven", + service = CompileOnSaveClassCleaner.class +) +public class GroovyCompileOnSaveClassCleaner implements CompileOnSaveClassCleaner { + + @Override + public void clean(Project project, boolean testDir) { + if (project.getProjectDirectory() == null) { + return; + } + + final FileObject targetClasses; + if (testDir) { + targetClasses = project.getProjectDirectory().getFileObject("target/test-classes"); //NOI18N + } else { + targetClasses = project.getProjectDirectory().getFileObject("target/classes"); //NOI18N + } + + for (SourceGroup sourceGroup : GroovySources.getGroovySourceGroups(ProjectUtils.getSources(project))) { + cleanSourceRoot(targetClasses, sourceGroup.getRootFolder(), testDir); + } + } + + private void cleanSourceRoot(FileObject targetClasses, FileObject sourceRoot, boolean testRoot) { + if (testRoot) { + // We want to clean test directories only + if (sourceRoot.getPath().endsWith("/src/test/groovy") || sourceRoot.getPath().endsWith("/src/test/java")) { //NOI18N + cleanClassFiles(targetClasses, sourceRoot, sourceRoot); + } + } else { + // We want to clean source directories only + if (sourceRoot.getPath().endsWith("/src/main/groovy") || sourceRoot.getPath().endsWith("/src/main/java")) { //NOI18N + cleanClassFiles(targetClasses, sourceRoot, sourceRoot); + } + } + } + + private void cleanClassFiles(FileObject targetClasses, FileObject rootFolder, FileObject folder) { + for (FileObject child : folder.getChildren()) { + if (child.isFolder()) { + cleanClassFiles(targetClasses, rootFolder, child); + } else { + deleteClassFile(targetClasses, rootFolder, child); + } + } + } + + private void deleteClassFile(FileObject targetClasses, FileObject rootFolder, FileObject sourceFile) { + final String sourceFilePath = sourceFile.getPath(); + + // Do not delete anything different than class files for groovy files! + if (!sourceFilePath.endsWith(".groovy")) { //NOI18N + return; + } + + // We are trying to get location of the same file (only with .class extension instead of .groovy) + // but in target/classes --> need to strip everything related to the source folder + // + // Instead of "/tmp/MavenApplication1/src/main/groovy/packagename/subpackage/SomeGroovyFile.groovy" + // we will get only "packagename/subpackage/SomeGroovyFile.groovy" + + final int indexOfFQN = rootFolder.getPath().length() + 1; + final String sourceFileFQN = sourceFilePath.substring(indexOfFQN); + + // Just changing the extension from "groovy" to "class" + final String classFilePath = sourceFileFQN.substring(0, sourceFileFQN.length() - 6) + "class"; //NOI18N + + // ..and finally finding the file in target/classes folder + final FileObject classFile = targetClasses.getFileObject(classFilePath); //NOI18N + if (classFile != null) { + try { + classFile.delete(); + } catch (IOException ex) { + Exceptions.printStackTrace(ex); + } + } + } +} diff --git a/maven.groovy/src/org/netbeans/modules/maven/groovy/GroovyCompileOnSaveSkipper.java b/maven.groovy/src/org/netbeans/modules/maven/groovy/GroovyCompileOnSaveSkipper.java new file mode 100644 --- /dev/null +++ b/maven.groovy/src/org/netbeans/modules/maven/groovy/GroovyCompileOnSaveSkipper.java @@ -0,0 +1,65 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2012 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2012 Sun Microsystems, Inc. + */ + +package org.netbeans.modules.maven.groovy; + +import org.netbeans.modules.maven.api.execute.RunConfig; +import org.netbeans.modules.maven.spi.cos.CompileOnSaveSkipper; +import org.openide.filesystems.FileObject; + +/** + * + * @author Martin Janicek + */ +@org.openide.util.lookup.ServiceProvider(service=CompileOnSaveSkipper.class) +public class GroovyCompileOnSaveSkipper implements CompileOnSaveSkipper { + + @Override + public boolean skip(RunConfig config, boolean includingTests, long timeStamp) { + final FileObject runFile = config.getSelectedFileObject(); + + if ("groovy".equals(runFile.getExt())) { //NOI18N + return true; + } + return false; + } +} diff --git a/maven/manifest.mf b/maven/manifest.mf --- a/maven/manifest.mf +++ b/maven/manifest.mf @@ -1,6 +1,6 @@ Manifest-Version: 1.0 OpenIDE-Module: org.netbeans.modules.maven/2 -OpenIDE-Module-Specification-Version: 2.61 +OpenIDE-Module-Specification-Version: 2.62 OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/maven/Bundle.properties OpenIDE-Module-Layer: org/netbeans/modules/maven/layer.xml AutoUpdate-Show-In-Client: false diff --git a/maven/src/org/netbeans/modules/maven/cos/CosChecker.java b/maven/src/org/netbeans/modules/maven/cos/CosChecker.java --- a/maven/src/org/netbeans/modules/maven/cos/CosChecker.java +++ b/maven/src/org/netbeans/modules/maven/cos/CosChecker.java @@ -85,6 +85,7 @@ import org.netbeans.modules.maven.customizer.CustomizerProviderImpl; import org.netbeans.modules.maven.customizer.RunJarPanel; import org.netbeans.modules.maven.execute.DefaultReplaceTokenProvider; +import org.netbeans.modules.maven.spi.cos.CompileOnSaveClassCleaner; import org.netbeans.modules.maven.spi.cos.CompileOnSaveSkipper; import org.netbeans.spi.java.classpath.support.ClassPathSupport; import org.netbeans.spi.project.ActionProvider; @@ -774,6 +775,10 @@ MavenProject mvn = prj.getMavenProject(); if (RunUtils.hasApplicationCompileOnSaveEnabled(project)) { touchCoSTimeStamp(mvn, false); + + for (CompileOnSaveClassCleaner cleaner : project.getLookup().lookupAll(CompileOnSaveClassCleaner.class)) { + cleaner.clean(project, false); + } } else { File f = getCoSFile(mvn, false); boolean doClean = f != null && f.exists(); @@ -788,6 +793,10 @@ } if (RunUtils.hasTestCompileOnSaveEnabled(project)) { touchCoSTimeStamp(mvn, true); + + for (CompileOnSaveClassCleaner cleaner : project.getLookup().lookupAll(CompileOnSaveClassCleaner.class)) { + cleaner.clean(project, true); + } } else { deleteCoSTimeStamp(mvn, true); } diff --git a/maven/src/org/netbeans/modules/maven/spi/cos/CompileOnSaveClassCleaner.java b/maven/src/org/netbeans/modules/maven/spi/cos/CompileOnSaveClassCleaner.java new file mode 100644 --- /dev/null +++ b/maven/src/org/netbeans/modules/maven/spi/cos/CompileOnSaveClassCleaner.java @@ -0,0 +1,68 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2012 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2012 Sun Microsystems, Inc. + */ + +package org.netbeans.modules.maven.spi.cos; + +import org.netbeans.api.project.Project; + +/** + * Enables to clean some class files even if the Compile on Save feature is enabled. + * + * Normally if the Compile on Save feature is enabled, class files aren't deleted + * when running application. Because of that they are not recompiled as the maven + * runner thinks they are already up to date. This is not desirable in a certain + * situations (e.g. we don't have CoS implemented for groovy files and thus we want + * to recompile their class files even if CoS is enabled). + * + * @author Martin Janicek + * @since 2.62 + */ +public interface CompileOnSaveClassCleaner { + + /** + * + * @param project current project + * @param testDir true if the we want to clean test directory, false if we + * want to clean source directory + */ + void clean(Project project, boolean testDir); +}