2

I am new in maven and I would like to execute 2 java file at the same time. I read a few posts in StackOverflow and because I do have limited knowledge with maven, I couldn't understand how to do it. I attached my pom.xml file here and the command line that I am using in mac terminal is:

mvn compile exec:java -Dexec.mainClass="org.parallel.ParseThread1"

I have another file named: ParseThread2 and every time I am running them separately like this:

mvn compile exec:java -Dexec.mainClass="org.parallel.ParseThread1"

mvn compile exec:java -Dexec.mainClass="org.parallel.ParseThread2"

I want to know if maven is capable of executing both at the same time.

Few posts that i read was:

How to execute multiple ant targets in maven

Executing multiple maven profiles

Maven Build multiple profiles in one go

My pom.xml file is:

<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>org.parallel</groupId>
  <artifactId>parallel</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>parallel</name>
  <url>http://maven.apache.org</url>

  <properties>
      <maven.compiler.source>1.9</maven.compiler.source>
      <maven.compiler.target>1.9</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </dependency>

    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.3.0</version>
    </dependency>


  </dependencies>

</project>
Bilgin
  • 499
  • 1
  • 10
  • 25
  • Could you please also describe the _underlying_ problem, i.e. what do you want to achieve by running two classes at the same time with Maven. Maybe we can find a better solution for your original problem. – J Fabian Meier Nov 09 '18 at 15:19
  • I am testing my few java files that each one doing a different job, and I want to run them at the same time to see how long it could take the entire project for execution. I used to do this in c++ using makefile but since maven works different, I don't know how I should do it. As an example, I am looking to run like: mvn compile exec:java -Dexec.mainClass="org.parallel.ParseThread1. ParseThread1". (This is only example of command line and i know it is not correct) – Bilgin Nov 09 '18 at 15:36
  • But why do you run them with Maven? Maven is a build tool, i.e. for compilation and packaging. Not for running your application. – J Fabian Meier Nov 09 '18 at 15:46
  • I really appreciate your time for helping me in this post. That's right. yes, Maven is a build tool and I am building my project with it because it makes the build process structured and makes my job much easier. If I don't use the maven, I have to make a .sh file to add all the compile java file in it and compile them one by one and it gets too much complicated. In this post i would like to learn if the maven has this capability of building two project at the same time. – Bilgin Nov 09 '18 at 15:53
  • You should build your project with it, but I understood that you run your project with Maven, which is not the intended use. Building two different projects on the same machine at the same time is tricky because you would need to separate local repositories. If you want to always build several jars in one go, you can construct a multi-module project. This can be built in paralell. – J Fabian Meier Nov 09 '18 at 15:58
  • Thank you for your comment. Do you have any suggestion on how the parallel built can be done? Any documentation you suggest? – Bilgin Nov 09 '18 at 17:46

1 Answers1

0

See this answer.

mvn compile
java -cp target/classes org.parallel.ParseThread1 &
java -cp target/classes org.parallel.ParseThread2 &
Wassadamo
  • 1,176
  • 12
  • 32