【问题标题】:extract out one file from zip in maven从maven中的zip中提取一个文件
【发布时间】:2015-06-30 04:22:35
【问题描述】:

我在名为 output 的文件夹下有一个 zip 文件。 zip 里面包含 3 个文件。

Project root/
    -output/
        -my.zip
    pom.xml

my.zip(包含 names.txt、schools.txt、teacher.txt)。

如何从 zip 中仅提取 names.txt 并将其放在 ma​​ven 中的 output/ 下?

===我尝试过的:===

我可以通过从项目根目录运行命令来实现它:

unzip -p output/my.zip names.txt > output/names.txt 

但是当我尝试使用 exec-maven-plugin 并运行 maven 时:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.4.0</version>
    <executions>
        <execution>
            <id>get names.txt</id>
            <phase>package</phase>
            <goals>
                <goal>exec</goal>
            </goals>
            <configuration>
                <executable>unzip</executable>
                <commandlineArgs>-p output/my.zip names.txt > output/names.txt</commandlineArgs> 
            </configuration>
        </execution>
    </executions>
</plugin>

我总是使用 exec-maven-plugin 无法执行目标

我的exec-maven-plugin 配置有什么问题,以及在 maven 中实现它的任何其他方式?

【问题讨论】:

    标签: maven maven-3 maven-plugin exec-maven-plugin


    【解决方案1】:

    你应该看看maven-dependenc-plugin,它提供了一个目标unpack

    <project>
       [...]
       <build>
         <plugins>
           <plugin>
             <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-dependency-plugin</artifactId>
             <version>2.10</version>
             <executions>
               <execution>
                 <id>unpack</id>
                 <phase>package</phase>
                 <goals>
                   <goal>unpack</goal>
                 </goals>
                 <configuration>
                   <artifactItems>
                     <artifactItem>
                       <groupId>junit</groupId>
                       <artifactId>junit</artifactId>
                       <version>3.8.1</version>
                       <type>jar</type>
                       <overWrite>false</overWrite>
                       <outputDirectory>${project.build.directory}/alternateLocation</outputDirectory>
                       <destFileName>optional-new-name.jar</destFileName>
                       <includes>**/*.class,**/*.xml</includes>
                       <excludes>**/*test.class</excludes>
                     </artifactItem>
                   </artifactItems>
                   <includes>**/*.java</includes>
                   <excludes>**/*.properties</excludes>
                   <outputDirectory>${project.build.directory}/wars</outputDirectory>
                   <overWriteReleases>false</overWriteReleases>
                   <overWriteSnapshots>true</overWriteSnapshots>
                 </configuration>
               </execution>
             </executions>
           </plugin>
         </plugins>
       </build>
       [...]
     </project>
    

    【讨论】:

    • 但 my.zip 不是本地 maven repo 中的工件。我也不想把它放到本地 maven repo 中。
    • 为什么不呢?会让您的生活更轻松。
    • 不幸的是,我们不能总是将在构建过程中必须处理(或如果您愿意使用)的文件放入存储库中,例如,如果我们需要从客户 ftp 检索它们。
    【解决方案2】:

    你可以用maven-antrun-plugin来做,通过这个例子我想你会有你的答案。

    <plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        <executions>
            <execution>
                <phase>package</phase>
                <configuration>
                    <tasks>
                        <unzip src="${project.basedir}/output/test.zip" dest="${project.basedir}/output/unzip">
                            <patternset>
                                <include name="**/*.xml"/>
                            </patternset>
                        </unzip>
                    </tasks>
                </configuration>
                <goals>
                    <goal>run</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    

    【讨论】:

      【解决方案3】:

      这可能是因为commandLineArgs 被给予的方式。

      例如:根据插件文档,标准输出应使用可选参数“outputFile”进行定向

      http://mojo.codehaus.org/exec-maven-plugin/exec-mojo.html#outputFile

      尝试执行一些简单的命令,例如 pwdls -la,然后返回到您的 unzip 示例。

      更新:

      您可能还需要考虑使用 XML CDATA 提供参数

      <sampleTag>
      <![CDATA[
          Some text with > and <
       ]]>
      </sampleTag>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多