Funding legislation for Maryland's most sought-after and elusive public works project, the dredging of Baltimore Harbor, and a similar project at Hampton Roads, Va., appeared headed for defeat last night, both victims of a threatened presidential veto of water projects throughout the nation.

House and Senate conferees reluctantly dropped more than 40 controversial water projects after the White House stood its ground, with officials asserting that President Reagan would veto an essential year-long funding bill if the projects remained a part of it.

"It's all over. The ports are history," said a disappointed aide to Rep. Clarence D. Long (D-Md.), who had fought for funding for the $314 million harbor-deepening enterprise in his district.

Legislators will be forced to start from scratch next session to seek the funds, and according to Rep. Steny H. Hoyer (D-Md.), "it could be a tough fight again." But Sen. John W. Warner (R-Va.) said he believes the work done this session established "recognition by senators of the need for these projects," which will make the job easier.

The conferees' agreement on the catchall spending bill was approved by the House last night, and Senate will vote today. Then it must go to the White House.

While the two states lost the crucial port measures, the massive catchall funding bill contained several prizes for the Washington area, including a provision designed to force the Reagan administration to lift its controversial 76.4-mile limit on Metro construction.

The Metro provision was aimed at overturning federal restrictions that have barred construction of major portions of the long-stalled Green Line and other sections of the proposed 101-mile rail system.

The bill also contained millions of federal dollars for other projects sought by the two jurisdictions.

But for Maryland legislators, the death of the port-funding measure was yet another setback in a 14-year struggle to begin a project that would bring jobs and lucrative coal export profits to the state by deepening the port and allowing fully loaded super coal carriers to enter it for the first time.

But local legislators were no match for the larger forces moving to kill all the water projects.

"The president has us in a box," said Hoyer. "There are 50 members in tough, close races who are not prepared to take another week to go through this process."

Other sections of the massive funding measure included at least one provision opposed by Northern Virginia congressmen. It would prohibit the U.S. Transportation Department from lowering the ceiling on the number of passengers using National Airport. Still, the bill included several other examples of federal largess for the area.

Maryland's share included: Nearly $30 million for highway and transit construction and repair, including $6.5 million for renovation and repair on Suitland Parkway; $15 million earmarked for Baltimore highways, and $7 million for the Northwest extension of the Baltimore subway system. About $1.1 million for continuation of studies of the Chesapeake Bay and of the endangered striped bass, or rockfish.

Federal funds headed for Virginia include: $3 million for the acquisition of additional parcels of land from private owners for the Manassas Battlefield, a state park whose boundaries were expanded in 1980. $1.5 million in funds, previously earmarked for another project, for design and engineering studies of a new concourse at Dulles Airport. The concourse would house new gates for planes at the airport, which needs expanded gate capacity, according to Warner aides.

The bill also includes a multimillion bailout for the Kennedy Center, which will be relieved of $33 million in interest owed to the federal government and freed of paying future interest for the next 33 years. The measure survived a last-ditch effort to derail it by Rep. Guy V. Molinari (R-N.Y.), who called it "an absolute ripoff for the American taxpayers."