You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@maven.apache.org by GitBox <gi...@apache.org> on 2022/10/28 15:38:58 UTC

[GitHub] [maven-help-plugin] broodjetom opened a new pull request, #76: Feature/mph 168

broodjetom opened a new pull request, #76:
URL: https://github.com/apache/maven-help-plugin/pull/76

   Following this checklist to help us incorporate your 
   contribution quickly and easily:
   
    - [x] Make sure there is a [JIRA issue](https://issues.apache.org/jira/browse/MPH) filed 
          for the change (usually before you start working on it).  Trivial changes like typos do not 
          require a JIRA issue.  Your pull request should address just this issue, without 
          pulling in other changes.
    - [x] Each commit in the pull request should have a meaningful subject line and body.
    - [x] Format the pull request title like `[MPH-XXX] - Fixes bug in ApproximateQuantiles`,
          where you replace `MPH-XXX` with the appropriate JIRA issue. Best practice
          is to use the JIRA issue title in the pull request title and in the first line of the 
          commit message.
    - [x] Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
    - [x] Run `mvn clean verify` to make sure basic checks pass. A more thorough check will 
          be performed on your pull request automatically.
    - [x] You have run the integration tests successfully (`mvn -Prun-its clean verify`).
   
   If your pull request is about ~20 lines of code you don't need to sign an
   [Individual Contributor License Agreement](https://www.apache.org/licenses/icla.pdf) if you are unsure
   please ask on the developers list.
   
   To make clear that you license your contribution under 
   the [Apache License Version 2.0, January 2004](http://www.apache.org/licenses/LICENSE-2.0)
   you have to acknowledge this by using the following check-box.
   
    - [x] I hereby declare this contribution to be licenced under the [Apache License Version 2.0, January 2004](http://www.apache.org/licenses/LICENSE-2.0)
   
    - [ ] In any other case, please file an [Apache Individual Contributor License Agreement](https://www.apache.org/licenses/icla.pdf). **I'm in the process.**
   
   ---
   
   This PR closes https://issues.apache.org/jira/browse/MPH-168.
   
   I have chosen to solve this issue using a new parameter `individual`. I chose this name because you are generating an effective POM for each project/module individually. It is already possible to generate an effective POM across for each project/module, but it will put it in a single result/file. Please, advice on this naming if you like it or what it should be changed to.
   
   I have not yet built the support of specifying `output` in combination with `individual`. For now, it will always output the effective POM to `${projectDirectory}/target/effective.pom.xml`. In the issue it mentions setting the `output` to `${project.build.directory}/effective-pom.xml`, but I'm not sure how to transform the variable part in it per project.
   
   IMO the code does not look very clean, I think some of it could be refactored, but I'm not sure in what way.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [maven-help-plugin] garretwilson commented on a diff in pull request #76: [MPH-168] effective-pom should support multi-module project

Posted by GitBox <gi...@apache.org>.
garretwilson commented on code in PR #76:
URL: https://github.com/apache/maven-help-plugin/pull/76#discussion_r1014281946


##########
src/main/java/org/apache/maven/plugins/help/EffectivePomMojo.java:
##########
@@ -140,24 +161,85 @@ public void execute()
         }
 
         String effectivePom = prettyFormat( w.toString(), encoding, false );
+        effectivePom = prettyFormatVerbose( effectivePom );
+        reportEffectivePom( effectivePom, output );
+    }
+
+    private void generateIndividualEffectivePom() throws MojoExecutionException
+    {
+        String encoding = project.getModel().getModelEncoding();
+        if ( shouldWriteAllEffectivePOMsInReactor() )
+        {
+            // outer root element
+            for ( MavenProject subProject : projects )
+            {
+                StringWriter w = new StringWriter();
+                XMLWriter writer =
+                        getPrettyPrintXMLWriterForEffectivePom( w, encoding );
+                writeHeader( writer );
+                writeEffectivePom( subProject, writer );
+                String effectivePom = prettyFormat( w.toString(), encoding, false );
+                effectivePom = prettyFormatVerbose( effectivePom );
+                File effectiveOutput = output == null ? null
+                        : getRelativeOutput( subProject );
+                reportEffectivePom( effectivePom, effectiveOutput );
+            }
+        }
+        else
+        {
+            StringWriter w = new StringWriter();
+            XMLWriter writer =
+                    getPrettyPrintXMLWriterForEffectivePom( w, encoding );
+            writeHeader( writer );
+            writeEffectivePom( project, writer );
+            String effectivePom = prettyFormat( w.toString(), encoding, false );
+            effectivePom = prettyFormatVerbose( effectivePom );
+            File effectiveOutput = output == null ? null
+                    : getRelativeOutput( project );
+            reportEffectivePom( effectivePom, effectiveOutput );
+        }
+    }
+
+    private File getRelativeOutput( MavenProject relativeProject )
+    {
+        String rawOutputPath = output.getPath();
+        String rawBasedirPath = project.getBasedir().getPath();
+        String result = rawOutputPath.contains( rawBasedirPath )

Review Comment:
   This brute-force searching of strings is not the correct way to process paths. Even in the best of worlds, there are all sorts of cases this won't work with (e.g. if the paths are not normalized, and contains `../`, etc. In addition you're using `contains()`, which even with this approach isn't correct (it looks even in the _middle_ of the string).
   
   Instead of searching raw strings, you should be using path manipulation methods for semantic `Path` objects. For example, there's a method made to do exactly what you want: [`Path.relativize()`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/nio/file/Path.html#relativize(java.nio.file.Path)). And if you don't have a `Path`, you can convert from the old `File` model to the new Java NIO `Path` model and back.
   
   If you're not used to working with semantic path objects, you might start at Oracle's [The Path Class](https://docs.oracle.com/javase/tutorial/essential/io/pathClass.html) tutorial, which includes [Path Operations](https://docs.oracle.com/javase/tutorial/essential/io/pathOps.html) explanations..



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [maven-help-plugin] broodjetom commented on pull request #76: [MPH-168] effective-pom should support multi-module project

Posted by GitBox <gi...@apache.org>.
broodjetom commented on PR #76:
URL: https://github.com/apache/maven-help-plugin/pull/76#issuecomment-1295279306

   I have tried some things with the `output`. The problem is that variables in the command line parameters are resolved before reaching the Mojo. So, when I do:
   ```
   mvn clean help:effective-pom -Dindividual -Doutput="${project.build.directory}/effective.pom.xml"
   ```
   I get the following output:
   ```
   [INFO] --- maven-help-plugin:3.3.1-SNAPSHOT:effective-pom (default-cli) @ triplem ---
   [INFO] Effective-POM written to: C:\Users\TomS\TempProjects\triplem\target\effective.pom.xml
   [INFO] Effective-POM written to: C:\Users\TomS\TempProjects\triplem\target\effective.pom.xml
   [INFO] Effective-POM written to: C:\Users\TomS\TempProjects\triplem\target\effective.pom.xml
   [INFO] Effective-POM written to: C:\Users\TomS\TempProjects\triplem\target\effective.pom.xml
   [INFO] Effective-POM written to: C:\Users\TomS\TempProjects\triplem\target\effective.pom.xml
   [INFO] Effective-POM written to: C:\Users\TomS\TempProjects\triplem\target\effective.pom.xml
   [INFO] Effective-POM written to: C:\Users\TomS\TempProjects\triplem\target\effective.pom.xml
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [maven-help-plugin] mthmulders commented on a diff in pull request #76: [MPH-168] effective-pom should support multi-module project

Posted by GitBox <gi...@apache.org>.
mthmulders commented on code in PR #76:
URL: https://github.com/apache/maven-help-plugin/pull/76#discussion_r1008690718


##########
src/main/java/org/apache/maven/plugins/help/EffectivePomMojo.java:
##########
@@ -140,24 +161,73 @@ public void execute()
         }
 
         String effectivePom = prettyFormat( w.toString(), encoding, false );
+        effectivePom = prettyFormatVerbose( effectivePom );
+        reportEffectivePom( effectivePom, output );
+    }
+
+    private void generateIndividualEffectivePom() throws MojoExecutionException
+    {
+        String encoding = project.getModel().getModelEncoding();
+        if ( shouldWriteAllEffectivePOMsInReactor() )
+        {
+            // outer root element
+            for ( MavenProject subProject : projects )
+            {
+                StringWriter w = new StringWriter();
+                XMLWriter writer =
+                        getPrettyPrintXMLWriterForEffectivePom( w, encoding );
+                writeHeader( writer );
+                writeEffectivePom( subProject, writer );
+                String effectivePom = prettyFormat( w.toString(), encoding, false );
+                effectivePom = prettyFormatVerbose( effectivePom );
+                File effectiveOutput = new File( subProject.getBuild().getDirectory() + "/effective.pom.xml" );
+                reportEffectivePom( effectivePom, effectiveOutput );
+            }
+        }
+        else
+        {
+            StringWriter w = new StringWriter();
+            XMLWriter writer =
+                    getPrettyPrintXMLWriterForEffectivePom( w, encoding );
+            writeHeader( writer );
+            writeEffectivePom( project, writer );
+            String effectivePom = prettyFormat( w.toString(), encoding, false );
+            effectivePom = prettyFormatVerbose( effectivePom );
+            File effectiveOutput = new File( project.getBuild().getDirectory() + "/effective.pom.xml" );
+            reportEffectivePom( effectivePom, effectiveOutput );
+        }
+    }
+
+    private String prettyFormatVerbose( String effectivePom )
+    {
         if ( verbose )
         {
             // tweak location tracking comment, that are put on a separate line by pretty print
             effectivePom = effectivePom.replaceAll( "(?m)>\\s+<!--}", ">  <!-- " );
         }
+        return effectivePom;
+    }
+
+    private PrettyPrintXMLWriter getPrettyPrintXMLWriterForEffectivePom( StringWriter w, String encoding )
+    {
+        return new PrettyPrintXMLWriter( w, StringUtils.repeat( " ", XmlWriterUtil.DEFAULT_INDENTATION_SIZE ),
+                encoding, null );
+    }
 
-        if ( output != null )
+    private void reportEffectivePom( String effectivePom, File effectiveOutput ) throws MojoExecutionException
+    {
+        if ( effectiveOutput != null )
         {
             try
             {
-                writeXmlFile( output, effectivePom );
+                writeXmlFile( effectiveOutput, effectivePom );
             }
             catch ( IOException e )
             {
-                throw new MojoExecutionException( "Cannot write effective-POM to output: " + output, e );
+                throw new MojoExecutionException( "Cannot write effective-POM to output: " + effectiveOutput, e );

Review Comment:
   ```suggestion
                   throw new MojoExecutionException( "Cannot write effective POM to output: " + effectiveOutput, e );
   ```



##########
src/main/java/org/apache/maven/plugins/help/EffectivePomMojo.java:
##########
@@ -140,24 +161,73 @@ public void execute()
         }
 
         String effectivePom = prettyFormat( w.toString(), encoding, false );
+        effectivePom = prettyFormatVerbose( effectivePom );
+        reportEffectivePom( effectivePom, output );
+    }
+
+    private void generateIndividualEffectivePom() throws MojoExecutionException
+    {
+        String encoding = project.getModel().getModelEncoding();
+        if ( shouldWriteAllEffectivePOMsInReactor() )
+        {
+            // outer root element
+            for ( MavenProject subProject : projects )
+            {
+                StringWriter w = new StringWriter();
+                XMLWriter writer =
+                        getPrettyPrintXMLWriterForEffectivePom( w, encoding );
+                writeHeader( writer );
+                writeEffectivePom( subProject, writer );
+                String effectivePom = prettyFormat( w.toString(), encoding, false );
+                effectivePom = prettyFormatVerbose( effectivePom );
+                File effectiveOutput = new File( subProject.getBuild().getDirectory() + "/effective.pom.xml" );
+                reportEffectivePom( effectivePom, effectiveOutput );
+            }
+        }
+        else
+        {
+            StringWriter w = new StringWriter();
+            XMLWriter writer =
+                    getPrettyPrintXMLWriterForEffectivePom( w, encoding );
+            writeHeader( writer );
+            writeEffectivePom( project, writer );
+            String effectivePom = prettyFormat( w.toString(), encoding, false );
+            effectivePom = prettyFormatVerbose( effectivePom );
+            File effectiveOutput = new File( project.getBuild().getDirectory() + "/effective.pom.xml" );
+            reportEffectivePom( effectivePom, effectiveOutput );
+        }
+    }
+
+    private String prettyFormatVerbose( String effectivePom )
+    {
         if ( verbose )
         {
             // tweak location tracking comment, that are put on a separate line by pretty print
             effectivePom = effectivePom.replaceAll( "(?m)>\\s+<!--}", ">  <!-- " );
         }
+        return effectivePom;
+    }
+
+    private PrettyPrintXMLWriter getPrettyPrintXMLWriterForEffectivePom( StringWriter w, String encoding )
+    {
+        return new PrettyPrintXMLWriter( w, StringUtils.repeat( " ", XmlWriterUtil.DEFAULT_INDENTATION_SIZE ),
+                encoding, null );
+    }
 
-        if ( output != null )
+    private void reportEffectivePom( String effectivePom, File effectiveOutput ) throws MojoExecutionException
+    {
+        if ( effectiveOutput != null )
         {
             try
             {
-                writeXmlFile( output, effectivePom );
+                writeXmlFile( effectiveOutput, effectivePom );
             }
             catch ( IOException e )
             {
-                throw new MojoExecutionException( "Cannot write effective-POM to output: " + output, e );
+                throw new MojoExecutionException( "Cannot write effective-POM to output: " + effectiveOutput, e );
             }
 
-            getLog().info( "Effective-POM written to: " + output );
+            getLog().info( "Effective-POM written to: " + effectiveOutput );

Review Comment:
   ```suggestion
               getLog().info( "Effective POM written to: " + effectiveOutput );
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [maven-help-plugin] garretwilson commented on a diff in pull request #76: [MPH-168] effective-pom should support multi-module project

Posted by GitBox <gi...@apache.org>.
garretwilson commented on code in PR #76:
URL: https://github.com/apache/maven-help-plugin/pull/76#discussion_r1014281946


##########
src/main/java/org/apache/maven/plugins/help/EffectivePomMojo.java:
##########
@@ -140,24 +161,85 @@ public void execute()
         }
 
         String effectivePom = prettyFormat( w.toString(), encoding, false );
+        effectivePom = prettyFormatVerbose( effectivePom );
+        reportEffectivePom( effectivePom, output );
+    }
+
+    private void generateIndividualEffectivePom() throws MojoExecutionException
+    {
+        String encoding = project.getModel().getModelEncoding();
+        if ( shouldWriteAllEffectivePOMsInReactor() )
+        {
+            // outer root element
+            for ( MavenProject subProject : projects )
+            {
+                StringWriter w = new StringWriter();
+                XMLWriter writer =
+                        getPrettyPrintXMLWriterForEffectivePom( w, encoding );
+                writeHeader( writer );
+                writeEffectivePom( subProject, writer );
+                String effectivePom = prettyFormat( w.toString(), encoding, false );
+                effectivePom = prettyFormatVerbose( effectivePom );
+                File effectiveOutput = output == null ? null
+                        : getRelativeOutput( subProject );
+                reportEffectivePom( effectivePom, effectiveOutput );
+            }
+        }
+        else
+        {
+            StringWriter w = new StringWriter();
+            XMLWriter writer =
+                    getPrettyPrintXMLWriterForEffectivePom( w, encoding );
+            writeHeader( writer );
+            writeEffectivePom( project, writer );
+            String effectivePom = prettyFormat( w.toString(), encoding, false );
+            effectivePom = prettyFormatVerbose( effectivePom );
+            File effectiveOutput = output == null ? null
+                    : getRelativeOutput( project );
+            reportEffectivePom( effectivePom, effectiveOutput );
+        }
+    }
+
+    private File getRelativeOutput( MavenProject relativeProject )
+    {
+        String rawOutputPath = output.getPath();
+        String rawBasedirPath = project.getBasedir().getPath();
+        String result = rawOutputPath.contains( rawBasedirPath )

Review Comment:
   This brute-force searching of strings is not the correct way to process paths. Even in the best of worlds, there are all sorts of cases this won't work with (e.g. if the paths are not normalized, and contains `../, etc. In addition you're using `contains()`, which even with this approach isn't correct (it looks even in the _middle_ of the string).
   
   Instead of searching raw strings, you should be using path manipulation methods for semantic `Path` objects. For example, there's a method made to do exactly what you want: [`Path.relativize()`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/nio/file/Path.html#relativize(java.nio.file.Path)). And if you don't have a `Path`, you can convert from the old `File` model to the new Java NIO `Path` model and back.
   
   If you're not used to working with semantic path objects, you might start at Oracle's [The Path Class](https://docs.oracle.com/javase/tutorial/essential/io/pathClass.html) tutorial, which includes [Path Operations](https://docs.oracle.com/javase/tutorial/essential/io/pathOps.html) explanations..



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [maven-help-plugin] broodjetom commented on pull request #76: [MPH-168] effective-pom should support multi-module project

Posted by GitBox <gi...@apache.org>.
broodjetom commented on PR #76:
URL: https://github.com/apache/maven-help-plugin/pull/76#issuecomment-1303892113

   If have changed it to where the effective POMs are written to standard out when the output flag is not set. 
   
   When the output flag is set, we need to interpret that as relative to the build directory of the (sub)project. A problem is actually that output flag will be parsed as a file, and thus contains the full absolute path (including the drive on Windows). Luckily, the project where we run the command from is available and provides a basedir. 
   
   I have used this to do some testing:
   ![image](https://user-images.githubusercontent.com/17497376/200032538-57251fc4-0bc9-4885-bd99-7a1e8d228cd9.png)
   You can see the full path on top (triplem).
   
   If you run the following command from the main module:
   
   ```
   mvn clean help:effective-pom -Dindividual -Doutput="effective/effective.pom.xml"
   ```
   
   |Parameter|Value|
   | --- | --- |
   | `basedir` | `C:\Users\TomS\TempProjects\triplem` |
   | `output` | `C:\Users\TomS\TempProjects\triplem\effective\effective.pom.xml` |
   
   To then get the relative output I made it to remove the basedir from the output path, meaning you are left with `effective\effective.pom.xml`.
   
   ```
   [INFO] Effective POM written to: C:\Users\TomS\TempProjects\triplem\target\effective\effective.pom.xml
   [INFO] Effective POM written to: C:\Users\TomS\TempProjects\triplem\drinks\cola\target\effective\effective.pom.xml
   [INFO] Effective POM written to: C:\Users\TomS\TempProjects\triplem\drinks\fanta\target\effective\effective.pom.xml
   [INFO] Effective POM written to: C:\Users\TomS\TempProjects\triplem\drinks\target\effective\effective.pom.xml
   [INFO] Effective POM written to: C:\Users\TomS\TempProjects\triplem\chocolate\bounty\target\effective\effective.pom.xml
   [INFO] Effective POM written to: C:\Users\TomS\TempProjects\triplem\chocolate\mars\target\effective\effective.pom.xml
   [INFO] Effective POM written to: C:\Users\TomS\TempProjects\triplem\chocolate\target\effective\effective.pom.xml
   ```
   
   Now run the following command from the main module:
   
   ```
   mvn clean help:effective-pom -Dindividual -Doutput="../effective.pom.xml"
   ```
   
   |Parameter|Value|
   | --- | --- |
   | `basedir` | `C:\Users\TomS\TempProjects\triplem` |
   | `output` | `C:\Users\TomS\TempProjects\effective.pom.xml` |
   
   Now the `output` does not actually contain the `basedir`. In that case I have currently implemented it to just look at the name of the output path, which now is `effective.pom.xml` and use that as a file name in the build directory. 
   
   ```
   [INFO] Effective POM written to: C:\Users\TomS\TempProjects\triplem\target\effective.pom.xml
   [INFO] Effective POM written to: C:\Users\TomS\TempProjects\triplem\drinks\cola\target\effective.pom.xml
   [INFO] Effective POM written to: C:\Users\TomS\TempProjects\triplem\drinks\fanta\target\effective.pom.xml
   [INFO] Effective POM written to: C:\Users\TomS\TempProjects\triplem\drinks\target\effective.pom.xml
   [INFO] Effective POM written to: C:\Users\TomS\TempProjects\triplem\chocolate\bounty\target\effective.pom.xml
   [INFO] Effective POM written to: C:\Users\TomS\TempProjects\triplem\chocolate\mars\target\effective.pom.xml
   [INFO] Effective POM written to: C:\Users\TomS\TempProjects\triplem\chocolate\target\effective.pom.xml
   ```
   
   As a conclusion, you are now able to specify a relative path, but there is a constraint on relative paths going up the file tree. Please let me know if you see another solution?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org