The Boston Red Sox have agreed to a five-year deal with J.D. Martinez, ending their stalemate with the free-agent slugger, a source told ESPN's Pedro Gomez.

The front-loaded deal is worth $110 million, with opt-out clauses after the second and third years, according to multiple reports. Martinez will be paid $25 million in 2018 and '19 before the first opt out, $22 million in 2020 before the second opt out, and $19 million in the last two years if he remains with the club.

The Red Sox, seeking to upgrade the middle of their lineup after finishing last in the American League in home runs in 2017, emerged as a potential suitor for Martinez earlier this offseason and reportedly offered him a five-year deal worth approximately $100 million last month.

But negotiations between Martinez and the Red Sox stalled, leading to speculation that the seven-year veteran could potentially return to the Arizona Diamondbacks.

He is the second high-profile client of agent Scott Boras to agree to a contract in the past few days. First baseman Eric Hosmer agreed to a contract with the San Diego Padres on Saturday.

Martinez, 30, bolsters a Red Sox team that went 93-69 and won the American League East last season but also finished with just 168 homers and a .407 team slugging percentage, second-lowest in the league.

The addition of Martinez, who finished third in the majors with a career-high 45 homers in 2017, also helps Boston counter the offseason blockbuster move by the rival New York Yankees, who acquired reigning National League MVP Giancarlo Stanton in a trade with the Miami Marlins.

The NL wild-card-winning Diamondbacks' July 18 acquisition of Martinez from the Detroit Tigers for three minor leaguers might go down as one of the best midseason pickups in baseball history.

Martinez had a thunderous second half to the season, and it only picked up steam down the stretch. In 62 games with the Diamondbacks, he batted .302 with 29 home runs and 65 RBIs.

In September, Martinez batted .404 (40-for-99) with 16 home runs (tying a NL record), eight doubles, 36 RBIs and 26 runs scored in 24 games.

Martinez played outfield in Detroit and Arizona, but is expected to serve primarily as the DH for Boston.