简体   繁体   中英

Does Maven support negative lookbehind regex in <packagingExcludes/>

I have the following regex:

.*(?<!min)(\.css)

Which is meant to match all CSS files which are not minified. This regex works when testing it on a Java regex tester . However, it does not work when using it with Maven WAR plugin's <packagingExcludes/> configuration parameter. I use it as follows:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <failOnMissingWebXml>false</failOnMissingWebXml>    
        <packagingExcludes>
            <![CDATA[
            %regex[.*(?<!min)(\.css)]
             ]]>
        </packagingExcludes>
    </configuration>
</plugin>

Does Maven WAR plugin support negative lookbehind regex? If so, how do I use it.

You don't need CDATA. Use this:

<packagingExcludes>
%regex[.*(?&lt;!min)(\.css)]>
</packagingExcludes>


'&lt;' is equal to the sign '<'

If you can avoid it by naming your CSS files without any . characters in except for the extension pieces ( .css or .min.css ) then you don't need a negative lookbehind; this will do:

^[^.]+\.css$

(I can't remember if the anchors are needed. I've put them in.)

Otherwise, whether negative lookbehind is supported depends on the RE library used. Java's built-in one does support that feature (as it is PCRE-derived), but some others don't. Given that Java's does support it, there's at least a reasonable chance that it will work.

Enough to be worth just trying instead of putting effort into guessing about it ahead of time.

My previous answer, which I deleted, was wrong.

The actual problem is that you have one closing bracket too much in your XML. On the last line you only need two closing brackets instead of three ( ]]> instead of ]]]> )

This works fine when it test it on my computer:

        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <packagingExcludes>
<![CDATA[
%regex[.*(?<!min)(\.css)]
]]>
                </packagingExcludes>
            </configuration>
        </plugin>

I'm using Apache Maven 3.0.3 (r1075438; 2011-03-01 01:31:09+0800) and the plugin version 2.4 . I tried with both jdk1.6.0_65 and with the jdk1.8 beta and get the same results with both. I get the same result with the plugin declaration that you updated in your question above.

I have a almost empty war project with just two files in the src/main/webapp folder: my.css and my-min.css .

The command I run is mvn clean install && jar tvf target/testweb-0.1.war (project and artifactID is called testweb )

The output is:

META-INF/
META-INF/MANIFEST.MF
WEB-INF/
WEB-INF/classes/
my-min.css
WEB-INF/web.xml
META-INF/maven/testweb/testweb/pom.xml
META-INF/maven/testweb/testweb/pom.properties

So it excludes my.css but it includes my-min.css .

What does your project structure look like?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM