开发者

maven build with war and jar pushing odd artifacts to internal repo

开发者 https://www.devze.com 2023-04-08 14:34 出处:网络
I have a maven project where I am building a war file, but I am also using the maven-jar-plugin to build a jar in the same project.

I have a maven project where I am building a war file, but I am also using the maven-jar-plugin to build a jar in the same project.

--DISCLAIMER-- I know this is not the 'correct' way to do this, but there are some other issues occurring when splitting this into a jar project and a separate war project with some 3rd party plugins.

I am seeing some strange behavior with this. Below is my project structure.

warproject
-src
--main
---webapp
----WEB-INF
-----web.xml
---java
----com.test.myclass
-----test.java
-pom.xml

When I build this project, i get the correct war and jar file in my target directory, however in my local .m2 repo something strange happens. The war file that is installed is named correctly war-jar-0.0.1-SNAPSHOT.war, however the contents of this file are the contents of my jar file. This also occurs if I do the inverse. i.e. if I setup my project to build a jar and use the maven-war-plugin to build the war, the archives in my target directory are correct, but my local repo has jar file with the contents of my war file. Below is the pom file I am using.

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.test</groupId>
  <artifactId>war-jar</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <jarName>${project.artifactId}-${project.version}-client</jarName>
                </configuration>
                <executions>
                    <execution>
                        <id>make-a-jar</id>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
      开发者_运维问答          </executions>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/java</directory>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
    </build>

</project>

The console output for this is the following, it shows that the jar is being uploaded as the war.

Installing /home/me/work/src/war-jar/target/war-jar-0.0.1-SNAPSHOT.jar to /home/me/.m2/repository/com/test/war-jar/0.0.1-SNAPSHOT/war-jar-0.0.1-SNAPSHOT.war

--UPDATE I got this working, but I had to change the phase of my 'make-a-jar' execution to install from package. This works fine and the correct artifacts are uploaded, but I am still confused as to why this makes a difference. Obviously the artifact is generated at a different lifecycle phase, and hence is not around at the time of the original install for the project, hence the wrong file is not uploaded. This seems like a 'hack' and I would like to understand why this is behaving this way.


I'm answering my own questions since I didn't get any information that helped me get to my solution. See my update on my original question for my solution.


This also works,

<plugin>
  <groupId>org.apache.maven.plugins</groupId> 
  <artifactId>maven-war-plugin</artifactId>
  <executions>
    <execution>
      <!--
        When also running maven-jar-plugin correct maven-war-plugin's ID
        from "default-war" to "default-jar"
      -->
      <id>default-jar</id>
      <phase>package</phase>
      <goals><goal>war</goal></goals>
      <configuration>
      ...
      </configuration>
    </execution>
  </executions>
</plugin>

Refer to http://maven.apache.org/guides/mini/guide-default-execution-ids.html

To figure out why your project behaves as is, analyze the Effective POM.


You need to specify the configurations for the maven-install-plugin to achieve this. Add the following plugin config under <build>.

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-install-plugin</artifactId>
            <executions>
                <execution>
                    <phase>install</phase>
                    <goals>
                        <goal>install-file</goal>
                    </goals>
                    <configuration>
                        <packaging>jar</packaging>
                        <artifactId>${project.artifactId}</artifactId>
                        <groupId>${project.groupId}</groupId>
                        <version>${project.version}</version>
                        <file>
                            ${project.build.directory}/${project.artifactId}-${project.version}.jar
                        </file>
                    </configuration>
                </execution>
            </executions>
        </plugin>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号