【问题标题】:How to change permission of jar packaged by maven? I am using maven assembly plugin如何更改maven打包的jar的权限?我正在使用 Maven 程序集插件
【发布时间】:2013-11-25 04:50:21
【问题描述】:

我正在使用 maven 程序集插件来打包一个包含所有依赖项的 jar。但是jar文件是不可执行的。如何更改 jar 的权限?

-rw-r--r--  1 e17490  ADPROD\Domain Users  12072889 Nov 12 14:16 com-foo-bar.jar

Pom.xml

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
          <archive>
            <manifest>
              <mainClass>foo.Main</mainClass>
            </manifest>
          </archive>
        </configuration>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>        
      </plugin>

【问题讨论】:

  • 一个 jar 可以是可执行的,但它的文件没有可执行标志。重要的是它包含什么。阅读教程了解什么是可执行 jar:docs.oracle.com/javase/tutorial/deployment/jar/appman.html
  • 谢谢!我只是没有尝试在关闭可执行标志的情况下执行。似乎工作。

标签: java maven maven-3 executable-jar maven-assembly-plugin


【解决方案1】:

使用maven:exec插件执行chmod

例如

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>${org.codehaus.mojo.version}</version>
    <executions>
       <execution>
            <id>script-chmod</id>
            <phase>install</phase>
            <goals>
                <goal>exec</goal>
            </goals>
            <configuration>
                <executable>chmod</executable>
                <arguments>
                    <argument>+x</argument>
                    <argument>your-assembled-jar.jar</argument>
                </arguments>
            </configuration>
        </execution>
    </executions>
</plugin>

【讨论】:

  • 这很棒!我对阶段进行了更改,因此它在任何资源可能已被新复制到输出目录之后在 compile 时间运行。对于参数,对于我的用例,我有三个:-R+x${project.build.outputDirectory}。谢谢!
【解决方案2】:

如果有 Java 支持,则可以在 Linux 上执行 Jar 文件。一些发行版已经带有这样的配置。您需要做的就是威胁 jar 文件,就好像它们是任何可执行文件一样,例如已编译的程序或脚本:

chmod 700 myJar.jar
./myJar.jar

所以这个问题确实有道理。

您需要注意,此解决方案可能无法跨平台移植。

根据this answer,需要在Maven上使用如下:

<fileSets>
    <fileSet>
      <directory>${basedir}/src/main/web</directory>
      <includes>
        <include>some_dir</include>
      </includes>
      <outputDirectory>web</outputDirectory>
      <fileMode>0777</fileMode>
      <directoryMode>0777</directoryMode>
    </fileSet>
  </fileSets>

如果这不起作用,您也可以使用 AntRun Plugin 从 Maven 调用 chmod Ant task

&lt;chmod file="${dist}/start.sh" perm="ugo+rx"/&gt;

【讨论】:

    猜你喜欢
    • 2010-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-06
    • 2016-05-16
    • 2013-03-13
    • 2016-12-19
    • 2018-11-06
    相关资源
    最近更新 更多