Why maven compilation doesn't work with “pom”

2019-05-03 08:14发布

I don't know why my maven build doesn't generate target/classes in current pom setting, the packaging type must be "pom" in my case, please advise what is wrong... Thanks!

<?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>

3条回答
贼婆χ
2楼-- · 2019-05-03 08:38

The compiler plugin is not bound to any phase in the maven lifecycle with packaging pom. You would have to configure an execution like you did for the assemby plugin:

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

If your sources are in a folder other than src/main/java you would have to configure this folder in the build section of your pom:

<build>
    <sourceDirectory>${basedir}/path/to/sources</sourceDirectory>
    <!-- plugins and other configuration -->
</build>
查看更多
疯言疯语
3楼-- · 2019-05-03 08:47

pom packaging is just to let other modules inherit common and regular configurations such as plugins, dependencies, contributors, developers,....and so on for child modules. Just remember that it won't go beyond validate phase.

This packaging is logical and not real one and so you should not put any real code or resources at that level. If you use junit in say 5 child modules, so rather than defining the dependency in 5 pom files then you can just do it in the parent pom with pom packaging, and you still can specify specific version in your module if you like to override what's in the parent pom. When you run the parent pom then the pom execution starts from parent to children and then all dependencies are retrieved from up to down.

That's how i understand pom packaging. So, If you have code with such packaging this means that your maven project structure need amendment. Only use packaging pom as a common configurations between multiple modules only

查看更多
一夜七次
4楼-- · 2019-05-03 08:53

By setting the packaging type to pom, you specify that nothing should be compiled. Maybe pom isn't the right packaging type for this artifact after all? It looks like your script would run fine as jar.

查看更多
登录 后发表回答