Bill
Bill

Reputation: 3209

IntelliJ CE + Maven package without the IDE

I tried to search "Build jar without IDE" without any success, if anyone have any pointers, I would be happy to RTFM.

I haven't touched Java in about 5 years now, and everything seems a little very different.

I have an IntelliJ Java application, standard Java application, with some maven dependencies. I can go to Build > build artifacts, and it would generate a jar for me in which I can run the application by java -jar out.jar

The version of the IDE: IntelliJ Community Edition 2016.1

The content of the pom.xml file:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>app</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.yaml</groupId>
            <artifactId>snakeyaml</artifactId>
            <version>1.17</version>
        </dependency>
    </dependencies>
</project>

However, how can I build this application with the commandline, as if I was pressing "build artifact" from the IDE?

Upvotes: 1

Views: 120

Answers (1)

heenenee
heenenee

Reputation: 20125

Build artifacts configured through the project structure settings are stored in IntelliJ's project files, and are not automatically available through the command line. To build the artifact through Maven, you should manually translate your IntelliJ artifact configuration to a Maven Shade Plugin configuration that creates an executable jar. Assuming that your existing artifact configuration simply contains everything in your project, that would look like this:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.4.3</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>com.example.MyMainClass</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Add that snippet before your </project> tag in your pom.xml, then run mvn package on the command line, and Maven will generate an app-1.0-SNAPSHOT.jar file in your target directory which can be run with java -jar. If you want to generate the same artifact from within IntelliJ, you can do that by accessing the package lifecycle phase from the Maven Projects Tool Window.

Upvotes: 1

Related Questions