2

I am trying to build a cli application with java and maven which contains two profile profileName1 and profileName2 in maven.

I have run the below command and trying to package the jar with all dependencies :

mvn -P profileName1 clean package

I have tried the below to process:

Process 1

<profiles>
    <profile>
        <id>profileName1</id>
        <properties>
            <main.class>
                com.sunkuet02.CommandLineInterface
            </main.class>
            <jar.name>profileName1</jar.name>
        </properties>
    </profile>
</profiles>

But above doesn't include dependencies to the jar file

Process 2

<profiles>
    <profile>
        <id>profileName1</id>
        <properties>
            <main.class>
                com.sunkuet02.CommandLineInterface
            </main.class>
            <jar.name>profileName1</jar.name>
        </properties>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <configuration>
                        <archive>
                            <manifest>
                                <addClasspath>true</addClasspath>
                            </manifest>
                        </archive>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

This also doesn't include dependencies with jar file.

Is there any way to do this ?

sunkuet02
  • 2,376
  • 1
  • 26
  • 33
  • 1
    Why do you need two profiles? Why not using maven-assembly-plugin (jar-with-dependencies or maven-shade-plugin)? – khmarbaise Dec 22 '17 at 06:55
  • `trying to package the jar with all dependencies`. What you did what just: adding the main class to the MANIFEST and changing the JAR name (process 1), and adding the dependencies to your MANIFEST (process 2) – Tome Dec 22 '17 at 08:27
  • @khmarbaise, I have used `maven-assembly-plugin` and this generates a jar containing all the dependencies, but it also creates a jar without any dependencies. Don't know why :( – sunkuet02 Dec 22 '17 at 09:37
  • @Tome, I have tried to use the same project for two CLI app. – sunkuet02 Dec 22 '17 at 09:38
  • 1
    @sunkuet02, the base Maven build will basically always generate a "simple" JAR for your project. If you want to also generate a JAR (or any other artifact) with all its dependencies, you will have to use a plugin for that, for instance the maven-assembly-plugin, as khmarbaise mentioned. That will generate another JAR, usually with a specific classifier. To tune this behavior, have a look there: https://stackoverflow.com/questions/4101750/disable-the-execution-default-jar – Tome Dec 22 '17 at 09:53
  • 1
    If you have a packaging `jar` defined the default jar without dependencies will be created. If you use the maven-assembly-plugin it will create a jar-with-dependency as a supplemental artifact. What is the problem with the simple jar file? If you like to prevent that best is to make a separate module (dist) which contains the configuration for the jar-with-dependencies and packaging `pom`... – khmarbaise Dec 22 '17 at 14:16
  • Got it, Thanks @Tome – sunkuet02 Dec 22 '17 at 15:59

0 Answers0