【问题标题】:Why maven compilation doesn't work with "pom" packaging type为什么 maven 编译不适用于“pom”包装类型
【发布时间】:2011-10-27 20:46:01
【问题描述】:

我不知道为什么我的 maven 构建在当前 pom 设置中没有生成目标/类,在我的情况下,包装类型必须是“pom”,请告知哪里出了问题...谢谢!

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0     http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.abc.sm.doctor</groupId>
<artifactId>smdoctor</artifactId>
<packaging>pom</packaging>
<version>${SMDOCTOR_VERSION}</version>
<name>sm doctor</name>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
    <plugins>   
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.5</source>
                <target>1.5</target>
                <debug>true</debug>
                <debuglevel>source,lines</debuglevel>
                <showDeprecation>true</showDeprecation>
                <archive>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <appendAssemblyId>false</appendAssemblyId>         
                <finalName>smdoctor</finalName> 
                <descriptors>
                    <descriptor>dist.xml</descriptor>
                    <descriptor>zip.xml</descriptor>
                </descriptors>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.7</version>
    <executions>
      <execution>
        <id>attach-artifacts</id>
        <phase>package</phase>
        <goals>
          <goal>attach-artifact</goal>
        </goals>
        <configuration>
          <artifacts>
            <artifact>
              <file>target/smdoctor.zip</file>
              <type>zip</type>
            </artifact>
          </artifacts>
        </configuration>
      </execution>
    </executions>
  </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>...</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <version>2.3.1</version>
        </plugin>
    </plugins>
</build>
<dependencies>
    ... 
     </dependencies>

【问题讨论】:

    标签: maven-2 target maven-assembly-plugin maven-compiler-plugin


    【解决方案1】:

    编译器插件没有绑定到 maven 生命周期中的任何阶段并打包 pom。您必须像为 assemby 插件所做的那样配置 execution

    <executions>
        <execution>
            <id>compile</id>
            <phase>compile</phase>
            <goals>
                 <goal>compile</goal>
            </goals>
        </execution>
    </executions>
    

    如果您的源代码位于 src/main/java 以外的文件夹中,则必须在 pom 的构建部分中配置此文件夹:

    <build>
        <sourceDirectory>${basedir}/path/to/sources</sourceDirectory>
        <!-- plugins and other configuration -->
    </build>
    

    【讨论】:

    • 还是没有运气,得到了这个:[INFO] [compiler:compile {execution: compile}] [INFO] 没有要编译的源,为什么没有源要编译??
    • 您的资源是否位于src/main/java?否则,您必须将build 下的sourceDirectory 元素设置为正确的目录。
    【解决方案2】:

    通过将打包类型设置为pom,您可以指定不编译任何内容。也许pom 毕竟不是这个工件的正确包装类型?看起来您的脚本可以像 jar 一样正常运行。

    【讨论】:

    • 你说得对,jar作为打包类型是没有问题的,但是不知何故我需要使用pom,我可以通过某种方式强制编译吗??
    • 听起来你在 Maven 中做错了,因为如果你需要打包 pom,这意味着通常你在根目录下构建了多模块,但根目录不应该生成 jar。如果你需要创建一个jar而不是使用包装类型jar...
    【解决方案3】:

    pom 打包只是为了让其他模块继承通用和常规的配置,例如子模块的插件、依赖项、贡献者、开发人员……等等。请记住,它不会超出验证阶段。

    这种包装是合乎逻辑的而不是真实的,因此您不应在该级别放置任何真实的代码或资源。如果您在说 5 个子模块中使用 junit,而不是在 5 个 pom 文件中定义依赖项,那么您可以在带有 pom 打包的父 pom 中执行它,如果您想覆盖什么,您仍然可以在模块中指定特定版本在父 pom 中。当您运行父 pom 时,pom 执行从父到子开始,然后从上到下检索所有依赖项。

    这就是我对 pom 包装的理解。所以,如果你有这样的包装代码,这意味着你的 Maven 项目结构需要修改。仅使用打包 pom 作为多个模块之间的通用配置

    【讨论】:

      猜你喜欢
      • 2011-12-03
      • 2012-02-09
      • 2012-01-22
      • 2012-06-12
      • 2014-01-09
      • 1970-01-01
      • 2020-05-05
      • 2012-11-23
      • 1970-01-01
      相关资源
      最近更新 更多