Home > Mobile >  Maven : Remove target folder after build
Maven : Remove target folder after build

Time:01-05

I have a Spring project with a Parent and 2 sub-modules

   Parent
    -- mod1
    -- mod2

I want to build from the parent but the mod1 should not create a target folder( or should get deleted immediately after the build succeeds). How can I achieve this?

My goal is to make it's brother(mod2) to absorb the jar into it's lib and not "expose" the target folder of mod1 outside.

I have gone through multiple SO posts and articles on net but could not find something to help me effectively.

mod1 pom is

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>library-complete</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>multi-module-library-complete</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>17</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

Note: removing the build tag did not achieve my goal either.

Any help is appreciated.

CodePudding user response:

The target directory is Maven's working directory (even with a plugin, there is probably not a good way to delete it while the build is running).

Instead, you should run mvn clean once the first build is finished, and build artifacts have been moved to another location.

@echo off

REM -------------------------------------------------------------------------------
REM -- Build the project
call mvn clean package
set "RETURN_VAL=%ERRORLEVEL%"
if [1]==[%RETURN_VAL%] goto error
if [0]==[%RETURN_VAL%] goto success

REM -------------------------------------------------------------------------------
REM -- Take any action required for a failed build
:error
goto end

REM -------------------------------------------------------------------------------
:success
REM -- Copy build artifacts to project root (or another location of your choice)
cp "./target/Project.jar" "./Project.jar"

REM -- Remove the target directory
call mvn clean
goto end

REM -------------------------------------------------------------------------------
:end
pause
  • Related