velo / maven-formatter-plugin

Maven2 Java Formatter Plugin - Exported from code.google.com/p/maven-java-formatter-plugin
Other
11 stars 7 forks source link

Enable Resource-loading for configFile #8

Closed uweschaefer closed 9 years ago

uweschaefer commented 9 years ago

We're currently using revelc/formatter-maven-plugin but would like to switch to this version, due to the m2e connector. However, we miss a feature from the original version, which is to load the configFile from the classpath. Our usecase is pushing the configs in a dependency artifact to keep it separate and share it over a bunch of projects.

Is this something you consider a good addition to this version? maybe it could just be integrate from the original. if not, we'd be happy to assist.

uweschaefer commented 9 years ago

DUH! never mind. just found out that Resource.forPath already provides this feature. sorry!

velo commented 9 years ago

=D

paulbandler commented 7 years ago

uweschaefer

Did you have success with loading formats from the classpath?

I've followed the pattern of configuration documented for the revelc earlier version but so far not succeeded. An example config would be very useful? I created a dependency on another (codeformats:jar) jar component that contained the formats, but despite encouraging looking debug, it failed:

[DEBUG] com.**.****:codeformats:jar:0.0.1-SNAPSHOT:runtime [DEBUG] com.marvinformatics.formatter:support:jar:1.8.1:compile [DEBUG] com.marvinformatics.formatter:jdt-core:jar:1.8.1:compile [DEBUG] com.marvinformatics.formatter:jsdt-core:jar:1.8.1:compile [DEBUG] commons-digester:commons-digester:jar:2.0:compile [DEBUG] commons-beanutils:commons-beanutils:jar:1.8.0:compile [DEBUG] commons-logging:commons-logging:jar:1.1.1:compile

from:

<build>
    <plugins>
        <plugin>
            <groupId>com.marvinformatics.formatter</groupId>
            <artifactId>formatter-maven-plugin</artifactId>
            <version>1.8.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>format</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <configFile>classpath:META-INF/codeformats/eclipse-neon-spaces4tabs.xml</configFile>  
                <configJsFile>classpath:META-INF/codeformats/javascript.xml</configJsFile>
                <compilerCompliance default-value="1.8">1.8</compilerCompliance>
                <compilerSource default-value="1.8">1.8</compilerSource>
                <compilerTargetPlatform default-value="1.8">1.8</compilerTargetPlatform>
                <compilerTarget default-value="1.8">1.8</compilerTarget>
                <overrideConfigCompilerVersion>false</overrideConfigCompilerVersion>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>com.******.*****</groupId>
                    <artifactId>codeformats</artifactId>
                    <version>0.0.1-SNAPSHOT</version>
                    <scope>compile</scope>
                </dependency>
            </dependencies>             
        </plugin>
    </plugins>

[ERROR] Failed to execute goal com.marvinformatics.formatter:formatter-maven-plugin:1.8.1:format (default) on project trademodel: Unknown resource at classpath:META-INF/codeformats/eclipse-neon-spaces4tabs.xml -> [Help 1] org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal com.marvinformatics.formatter:formatter-maven-plugin:1.8.1:format (default) on project trademodel: Unknown resource at classpath:META-INF/codeformats/eclipse-neon-spaces4tabs.xml at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:212) at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153) at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80) at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51) at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128) at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307) at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193) at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106) at org.apache.maven.cli.MavenCli.execute(MavenCli.java:863) at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:288) at org.apache.maven.cli.MavenCli.main(MavenCli.java:199) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:483) at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289) at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229) at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415) at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356) Caused by: org.apache.maven.plugin.MojoExecutionException: Unknown resource at classpath:META-INF/codeformats/eclipse-neon-spaces4tabs.xml at com.marvinformatics.formatter.FormatterMojo.getOptionsFromConfigFile(FormatterMojo.java:586) at com.marvinformatics.formatter.FormatterMojo.getFormattingOptions(FormatterMojo.java:553) at com.marvinformatics.formatter.FormatterMojo.createCodeFormatter(FormatterMojo.java:540) at com.marvinformatics.formatter.FormatterMojo.execute(FormatterMojo.java:261) at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134) at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:207) ... 20 more Caused by: com.marvinformatics.formatter.support.io.Resource$UnknownResourceException: Unknown resource at classpath:META-INF/codeformats/eclipse-neon-spaces4tabs.xml at com.marvinformatics.formatter.support.io.Resource$ClasspathResource.toInputStream(Resource.java:134) at com.marvinformatics.formatter.support.io.Resource.asInputStream(Resource.java:51) at com.marvinformatics.formatter.FormatterMojo.getOptionsFromConfigFile(FormatterMojo.java:575)

Do I need to do something else to get this jar dependency into the formatter plugin's class-path?

Legioth commented 7 years ago

@paulbandler Since the classpath: protocol does getClass().getResourceAsStream(getNativePath()), you'd need to include a leading slash in the path to make it resolve relative to the root of the classpath instead of relative to the com.marvinformatics.formatter.support.io package.

You should thus use <configFile>classpath:/META-INF/codeformats/eclipse-neon-spaces4tabs.xml</configFile> and correspondingly for the js config file.

paulbandler commented 7 years ago

Legioth - thank you very much - it works now - made my day!